简体   繁体   English

如何在Flutter中的单例内部使用Provider.of(...)?

[英]How to use Provider.of(…) inside a singleton in Flutter?

I've this widget somewhere deep inside my widget tree: 我将这个widget放在小部件树的深处:

Widget build(BuildContext context) {
return ChangeNotifierProvider(
  builder: (context) => TimersModel(context: context),
  child: Scaffold(...

TimersModel get the context: TimersModel获取上下文:

class TimersModel extends ChangeNotifier {
  final BuildContext context;
  NotificationsService _notificationsService;

  TimersModel({@required this.context}) {
    _notificationsService = NotificationsService(context: context);
  }

And instantiates this NotificationsService singleton for the first and only time: 并首次实例化此NotificationsService单例:

class NotificationsService {
  static FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin;

  final BuildContext context;

  static NotificationsService _instance;

  factory NotificationsService({@required BuildContext context}) {
    _instance ??= NotificationsService._internalConstructor(context: context);
    return _instance;
  }

  NotificationsService._internalConstructor({@required this.context}) {

As you can see, this is a singleton for the FlutterLocalNotificationsPlugin 如您所见,这是FlutterLocalNotificationsPlugin的单例

Problem is that if I call Provider.of<TimersModel>(context)... from this singleton, although it's getting the right context, it's always throwing ProviderNotFoundError . 问题是,如果我从此单例调用Provider.of<TimersModel>(context)... ,尽管它获取正确的上下文,但它始终会抛出ProviderNotFoundError

If I place a breakpoint on this code from Provider: 如果我在提供程序的此代码上放置了一个断点:

static T of<T>(BuildContext context, {bool listen = true}) {
    // this is required to get generic Type
    final type = _typeOf<InheritedProvider<T>>();
    final provider = listen
        ? context.inheritFromWidgetOfExactType(type) as InheritedProvider<T>
        : context.ancestorInheritedElementForWidgetOfExactType(type)?.widget
            as InheritedProvider<T>;

    if (provider == null) {
      throw ProviderNotFoundError(T, context.widget.runtimeType);
    }

    return provider._value;
  }

The context ChangeNotifierProvider and type TimersModel are right. 上下文ChangeNotifierProvider和类型TimersModel是正确的。 But provider is always null. 但是provider始终为null。

I know the singleton is not a widget and, of course, it's not in the widget tree. 我知道单例不是小部件,当然,它不在小部件树中。

But shouldn't I be able to call Provider.of<TimersModel>(context)... from anywhere as long as I provide the right context and type? 但是,只要提供正确的上下文和类型,我是否应该可以从任何地方调用Provider.of<TimersModel>(context)...

Or should this work and I'm doing something wrong? 还是应该这样做,而我做错了什么?

As Provider does the lookup by type, try giving your ChangeNotifierProvider a type when you return it in your build method: 由于Provider按类型进行查找,因此在构建方法中返回ChangeNotifierProvider时,请尝试为其提供一种类型:

return ChangeNotifierProvider<TimersModel>(...);

I can imagine that Provider simply doesn't find an instance for TimersModel because you didn't declare a provider of that type. 我可以想象Provider根本找不到TimersModel的实例,因为您没有声明该类型的提供者。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM