Flutter 颤振:从窗口小部件树外部访问提供程序

Flutter 颤振:从窗口小部件树外部访问提供程序,flutter,push-notification,provider,flutter-state,Flutter,Push Notification,Provider,Flutter State,在我的flatter应用程序中,我必须在推送通知到达后立即更新提供商持有的状态,以便可以重建UI。 PushNotifications服务是这样一个类(不是小部件): class PushNotifications { ... Future<void> init() async { _firebaseMessaging.configure(onMessage: (Map<String, dynamic> data) { // here

在我的flatter应用程序中,我必须在推送通知到达后立即更新提供商持有的状态,以便可以重建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

    });
  }
}

类推送通知{
...
Future init()异步{
_配置(onMessage:(映射数据){
//在这里,我从通知接收数据,我必须将其添加到提供者
});
}
}
这是我的简单提供者:

class NewsProvider with ChangeNotifier {
  List<News> _news;
  get news => _news;

  NewsProvider();

  void addNews(News news) {
    _news.add(news);
    notifyListeners();
  }
}

使用ChangeNotifier为新闻提供者分类{
新闻列表;
获取新闻=>\u新闻;
新闻提供者();
无效添加新闻(新闻){
_添加(新闻);
notifyListeners();
}
}

由于PushNotifications类没有上下文,我想不出如何实现这一点。

请查看Riverpod,它基本上是一个没有限制的提供者。Riverpod与提供者非常相似,也是由同一作者雷米·罗塞莱(Remi Rousselet)创作的

据作者说:

Riverpod项目可被视为对提供商的重写,以进行不可能的改进

Riverpod官方网站:


Riverpod on Pub.dev:

请查看Riverpod,它基本上是一个没有限制的提供商。Riverpod与提供者非常相似,也是由同一作者雷米·罗塞莱(Remi Rousselet)创作的

据作者说:

Riverpod项目可被视为对提供商的重写,以进行不可能的改进

Riverpod官方网站:

Pub.dev上的Riverpod: