title: How to use Provider.of(...) inside a singleton in Flutter?
body: <p>I've this <code>widget</code> somewhere deep inside my widget tree:</p>
  
  <pre><code>Widget build(BuildContext context) {
  return ChangeNotifierProvider(
    builder: (context) =&gt; TimersModel(context: context),
    child: Scaffold(...
  </code></pre>
  
  <p><code>TimersModel</code> get the context:</p>
  
  <pre><code>class TimersModel extends ChangeNotifier {
    final BuildContext context;
    NotificationsService _notificationsService;
  
    TimersModel({@required this.context}) {
      _notificationsService = NotificationsService(context: context);
    }
  </code></pre>
  
  <p>And instantiates this <code>NotificationsService</code> singleton for the first and only time:</p>
  
  <pre><code>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}) {
  </code></pre>
  
  <p>As you can see, this is a singleton for the <code>FlutterLocalNotificationsPlugin</code></p>
  
  <p>Problem is that if I call <code>Provider.of&lt;TimersModel&gt;(context)...</code> from this singleton, although it's getting the right context, it's always throwing <code>ProviderNotFoundError</code>.</p>
  
  <p>If I place a breakpoint on this code from Provider:</p>
  
  <pre><code>static T of&lt;T&gt;(BuildContext context, {bool listen = true}) {
      // this is required to get generic Type
      final type = _typeOf&lt;InheritedProvider&lt;T&gt;&gt;();
      final provider = listen
          ? context.inheritFromWidgetOfExactType(type) as InheritedProvider&lt;T&gt;
          : context.ancestorInheritedElementForWidgetOfExactType(type)?.widget
              as InheritedProvider&lt;T&gt;;
  
      if (provider == null) {
        throw ProviderNotFoundError(T, context.widget.runtimeType);
      }
  
      return provider._value;
    }
  </code></pre>
  
  <p>The context <code>ChangeNotifierProvider</code> and type <code>TimersModel</code> are right. But provider is always null.</p>
  
  <p>I know the singleton is not a widget and, of course, it's not in the widget tree.</p>
  
  <p>But shouldn't I be able to call <code>Provider.of&lt;TimersModel&gt;(context)...</code> from anywhere as long as I provide the right context and type?</p>
  
  <p>Or should this work and I'm doing something wrong?</p>