-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathnews_change_notifier.dart
More file actions
33 lines (26 loc) · 1.11 KB
/
news_change_notifier.dart
File metadata and controls
33 lines (26 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import 'package:flutter/material.dart';
import 'package:flutter_testing_tutorial/article.dart';
import 'package:flutter_testing_tutorial/news_service.dart';
/// A class that extends [ChangeNotifier] to manage and notify listeners about news articles.
class NewsChangeNotifier extends ChangeNotifier {
/// The service responsible for fetching news articles.
final NewsService _newsService;
/// Creates an instance of [NewsChangeNotifier] with the given [NewsService].
NewsChangeNotifier(this._newsService);
/// A list of articles fetched from the news service.
List<Article> _articles = [];
/// Returns the list of articles.
List<Article> get articles => _articles;
/// Indicates whether articles are currently being loaded.
bool _isLoading = false;
/// Returns true if articles are being loaded, false otherwise.
bool get isLoading => _isLoading;
/// Fetches articles from the news service and updates the state.
Future<void> getArticles() async {
_isLoading = true;
notifyListeners();
_articles = await _newsService.getArticles();
_isLoading = false;
notifyListeners();
}
}