ProviderNotFoundException: Could not find the correct Provider above this widget
Error: Could not find the correct Provider<Counter> above this Consumer<Counter> WidgetShort 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.
Fix it fast
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 run2Match what you see, then jump to its fix
- 1
The 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
The error only appears after Navigator.push to a new page, or in a dialog or bottom sheetThe provider is in another routeFix - 3
The same build method creates the provider and reads itThe context belongs to the widget that creates the providerFix - 4
The provider exists in the tree, but the message shows a different Provider<T>The type in watch or read differs from the type providedFix
- 1
What does the error look like?
Error: Could not find the correct Provider<Counter> above this Consumer<Counter> WidgetThis 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 root2Fix
Do a hot restart, or stop the app and run it again.
# stop the app, then start it again
flutter run3Verify
The screen opens without the exception.
flutter runConfirmed by raw.githubusercontent.com
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(" lib2Fix
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
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" lib2Fix
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 runConfirmed by raw.githubusercontent.com
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<" lib2Fix
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 runConfirmed by pub.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix