Flutter: `Provider.of(context)` cannot be found when it is a `FutureProvider()`

Issue

I have a provider in my widget tree and I call Provider.of<List<...>>(context) to find it. If it is a provider of any type it works fine, however, as soon as change the provider from a Provider() (for example) to a FutureProvider() it doesn’t work.

I haven’t changed any widgets in the tree and haven’t changed their position in the navigator. Provider.of() works fine but once I set it to be a FutureProvider() then it doesn’t work.

Edit: My code looks something like this:

inside widget build:

return FutureProvider(
      initialData: [],
      create: (_) =>
          DatabaseService(uid: _auth.getUser()!.uid).getJournalEntries(),
      catchError: (context, error) {
        print(error.toString());
      },
      ...
}

Then one of the children is another widget and this is its build function:

List<JournalEntryData> entries =
        Provider.of<List<JournalEntryData>>(context);
    return ElevatedButton(
        onPressed: () {
          print(entries);
        },
        child: Text('print provider data'));
}

I get the following error:

Error: Could not find the correct Provider<List> above this TestButton Widget

This happens because you used a BuildContext that does not include the provider
of your choice. There are a few common scenarios:

  • You added a new provider in your main.dart and performed a hot-reload.
    To fix, perform a hot-restart.

  • The provider you are trying to read is in a different route.

    Providers are "scoped". So if you insert of provider inside a route, then
    other routes will not be able to access that provider.

  • You used a BuildContext that is an ancestor of the provider you are trying to read.

    Make sure that TestButton is under your MultiProvider/Provider<List>.
    This usually happens when you are creating a provider and trying to read it immediately.

    For example, instead of:

    Widget build(BuildContext context) {
      return Provider<Example>(
        create: (_) => Example(),
        // Will throw a ProviderNotFoundError, because `context` is associated
        // to the widget that is the parent of `Provider<Example>`
        child: Text(context.watch<Example>()),
      ),
    }
    

    consider using builder like so:

    Widget build(BuildContext context) {
      return Provider<Example>(
        create: (_) => Example(),
        // we use `builder` to obtain a new `BuildContext` that has access to the provider
        builder: (context) {
          // No longer throws
          return Text(context.watch<Example>()),
        }
      ),
    }
    

The problem is that there is a provider in the widget tree:
Widget Tree

Replacing the FutureProvider() with any other type of provider makes it work, but I need a FutureProvider().

Solution

I didn’t define the type of the FutureProvider() so it was dynamic but in my Provider.of() call I specified a type which caused the issue. Specifiying the type of the FutureProvider() should solve the issue.

Answered By – Hady

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.