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.