Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

ProviderNotFoundException: Could not find the correct Provider above this widget

ErrorError: Could not find the correct Provider<Counter> above this Consumer<Counter> Widget

Short answer

The BuildContext you used does not sit below the provider. Provider looks up the widget tree from that context. Common causes: a hot reload after adding the provider, a provider that lives in a different route, or a context above the provider in the same build method. Hot restart, move the provider up, or use a builder.

  • Applies to provider 6.x, Flutter 3.3x, Dart 3.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the type in the message: Provider<T> is what is missing and the widget name is where it was looked up. Then do a full hot restart before changing code, since the first cause below needs only that.

    # stop the app, then
    flutter run
  2. 2Match what you see, then jump to its fix

    1. 1The error appears right after you added a provider at the top of the app and hot reloadedHot reload after adding the provider in main.dartFix
    2. 2The error only appears after Navigator.push to a new page, or in a dialog or bottom sheetThe provider is in another routeFix
    3. 3The same build method creates the provider and reads itThe context belongs to the widget that creates the providerFix
    4. 4The provider exists in the tree, but the message shows a different Provider<T>The type in watch or read differs from the type providedFix

What does the error look like?

  • Error: Could not find the correct Provider<Counter> above this Consumer<Counter> Widget
  • This happens because you used a `BuildContext` that does not include the provider of your choice.
  • Provider<Counter> not found for Consumer<Counter>

Cause 1 of 4

Hot reload after adding the provider in main.dart

1Check

A hot reload does not rebuild the widget tree above the screen you are on, so a provider added to main.dart is not there yet.

git diff --stat  # confirm the change was in main.dart or the app root

2Fix

Do a hot restart, or stop the app and run it again.

# stop the app, then start it again
flutter run

3Verify

The screen opens without the exception.

flutter run

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 4

The provider is in another route

1Check

Providers are scoped to the widget subtree they wrap. A provider created inside one route is not visible to a route pushed from it, because the new route is not below it.

grep -rn "ChangeNotifierProvider\|Provider(" lib

2Fix

Create the provider above MaterialApp so every route is under it.

void main() => runApp(
      ChangeNotifierProvider(
        create: (_) => Counter(),
        child: const MyApp(),
      ),
    );

3Verify

Push the route again and read the value there.

Text('${context.watch<Counter>().value}')

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 4

The context belongs to the widget that creates the provider

1Check

In that build method the context is above the provider, so the lookup never sees it. The widget you read from must be under the provider.

grep -rn "context.watch\|context.read\|Provider.of" lib

2Fix

Use the builder parameter of the provider, which gives a new context below it, or move the reading code into a child widget.

ChangeNotifierProvider(
  create: (_) => Counter(),
  builder: (context, child) => Text('${context.watch<Counter>().value}'),
)

3Verify

The widget builds and updates when the value changes.

flutter run

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 4

The type in watch or read differs from the type provided

1Check

Lookups match by type and take the closest ancestor of that type. A widget obtains only the closest ancestor of the requested type, and the package notes that an optional value must be read as a nullable type, for example watch<int?>.

grep -rn "Provider<\|watch<\|read<" lib

2Fix

Give the provider and the reader the same explicit type argument, and give providers distinct types when you need more than one of a kind.

Provider<Repo>(create: (_) => HttpRepo(), child: const MyApp())
// reader
final repo = context.read<Repo>();

3Verify

The lookup resolves with no exception.

flutter run

Confirmed by pub.dev

Did this fix it?

How do I stop it coming back?

  • Put app-wide providers above MaterialApp.
  • Always write the generic type on create and on every watch and read.
  • Read context.read in callbacks and context.watch in build, never context.watch in initState.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.