In my Flutter app I have to update the state hold by a Provider as soon as a push notification arrives so that the UI can be rebuilt. The PushNotifications service is a class (not a widget) like that:
class PushNotifications {
...
Future<void> init() async {
_firebaseMessaging.configure(onMessage: (Map<String, dynamic> data) {
// here I receive the data from the notification that I have to add to the provider
});
}
}
And this is my simple provider:
class NewsProvider with ChangeNotifier {
List<News> _news;
get news => _news;
NewsProvider();
void addNews(News news) {
_news.add(news);
notifyListeners();
}
}
I can't come up with how I can achive that since the PushNotifications class doesn't have a context.
Please check out Riverpod which is basically Provider without its limitation. Riverpod is very similar to Provider and also created by the same author Remi Rousselet.
According to the author :
Riverpod project can be considered as a rewrite of provider to make improvements that would be otherwise impossible.
Official website of Riverpod : https://riverpod.dev/
Riverpod on Pub.dev : https://pub.dev/packages/riverpod
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.