简体   繁体   English

Flutter:从小部件树外部访问提供者

[英]Flutter: access provider from outside the widget tree

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.在我的 Flutter 应用程序中,我必须在推送通知到达时立即更新 Provider 保持的状态,以便可以重建 UI。 The PushNotifications service is a class (not a widget) like that: 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

    });
  }
}

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.由于 PushNotifications 类没有上下文,我无法想出如何实现这一点。

Please check out Riverpod which is basically Provider without its limitation.请查看 Riverpod,它基本上是 Provider,没有它的限制。 Riverpod is very similar to Provider and also created by the same author Remi Rousselet. Riverpod 与 Provider 非常相似,也是由同一作者 Remi Rousselet 创建的。

According to the author :根据作者的说法:

Riverpod project can be considered as a rewrite of provider to make improvements that would be otherwise impossible. Riverpod项目可以被视为对供应商的重写,以进行原本不可能进行的改进。

Official website of Riverpod : https://riverpod.dev/ Riverpod官方网站: https ://riverpod.dev/

Riverpod on Pub.dev : https://pub.dev/packages/riverpod Pub.dev 上的 Riverpod: https ://pub.dev/packages/riverpod

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过 addPostFrameCallback 的 Flutter Provider 访问表示小部件在小部件树之外,但 flutter inspector 显示否则 - Flutter Provider access via addPostFrameCallback says widget is outside the widget tree but flutter inspector shows otherwise Flutter:在小部件树外的另一个 class 中调用 Provider 方法 - Flutter: Invoking Provider method in another class outside of the widget tree 试图从小部件树外部监听提供者公开的值 - Tried to listen to a value exposed with provider, from outside of the widget tree 在此提供程序示例中的此简单提供程序错误中,尝试从小部件树外部侦听提供程序公开的值 - Tried to listen to a value exposed with provider, from outside of the widget tree in this simple provider error in this provider example Flutter state 管理,从外部小部件树触发 state 更改 - Flutter state management, trigger state change from outside widget tree 在小部件树外调用提供者方法 - Calling a provider method outside the widget tree 如何在小部件树/小部件 class 之外调用riverpod 提供程序? - How to call a riverpod provider outside of widget tree/widget class? Flutter:如何在子小部件的构建上下文之外访问 function 中的“提供者”值? - Flutter: How to access 'provider' values in a function outside the build context of the child widget? 试图从小部件树外部侦听提供者公开的值。 但我想听 - Tried to listen to a value exposed with provider, from outside of the widget tree. But I want to listen to it EXCEPTION CAUGHT BY GESTURE 试图从小部件树的外部监听提供者公开的值 - EXCEPTION CAUGHT BY GESTURE Tried to listen to a value exposed with provider, from outside of the widget tree
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM