【发布时间】:2021-02-20 19:18:04
【问题描述】:
在我的 Flutter 应用程序中,我必须在推送通知到达后立即更新 Provider 的状态保持,以便可以重建 UI。 PushNotifications 服务是一个类似的类(不是小部件):
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
});
}
}
这是我的简单提供者:
class NewsProvider with ChangeNotifier {
List<News> _news;
get news => _news;
NewsProvider();
void addNews(News news) {
_news.add(news);
notifyListeners();
}
}
由于 PushNotifications 类没有上下文,我无法想出如何实现这一点。
【问题讨论】:
-
在这里查看我的答案,也许这可能是一个解决方案stackoverflow.com/a/69993917/1095610
标签: flutter push-notification provider flutter-state