flutter_bloc cannot find my Bloc: BlocProvider.of() called with a context that does not contain it
BlocProvider.of() called with a context that does not contain a CounterBloc.Short answer
BlocProvider.of() and context.read() look upward from the context you give them. The error means no BlocProvider of that exact type sits above that context. Usually the context belongs to the widget that creates the provider, or to a new route that was pushed outside it. Move the lookup below the provider, or pass the bloc on with BlocProvider.value.
Fix it fast
1Find out which cause you have
Read the type in the message (the name after 'does not contain a') and look at The context used was line. Then list every place that provides and reads that type, and compare where each one sits in the tree.
grep -rn "BlocProvider\|context.read<\|context.watch<" lib/2Match what you see, then jump to its fix
- 1
The error appears on the first read, in the same build method that contains BlocProvider(create: ...)The context belongs to the widget that builds the BlocProviderFix - 2
The first screen works, and the error appears after Navigator.push to a second screenA new route was pushed, and the bloc was provided only to the previous oneFix - 3
The message names a type that you have a BlocProvider for, yet the lookup failsThe type that is read is not the type that was providedFix
- 1
What does the error look like?
BlocProvider.of() called with a context that does not contain a CounterBloc.No ancestor could be found starting from the context that was passed to BlocProvider.of<CounterBloc>().This can happen if the context you used comes from a widget above the BlocProvider.
Cause 1 of 3
The context belongs to the widget that builds the BlocProvider
1Check
A BuildContext points at a position in the tree and sees only what is above that position. The build method that creates a BlocProvider owns a context above it, so anything read with that context cannot see the provider it has just made. The same goes for a sibling, such as an AppBar, that sits beside the provider and not under it.
grep -rn -A12 "BlocProvider(" lib/ | grep "context\."2Fix
Read the bloc from a context that is below the provider. A small separate widget is the clearest way; a Builder does the same inline.
class CounterPage extends StatelessWidget {
const CounterPage({super.key});
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => CounterBloc(),
child: Builder(
builder: (innerContext) => ElevatedButton(
onPressed: () => innerContext.read<CounterBloc>().add(Increment()),
child: const Text('Add'),
),
),
);
}
}3Verify
Tap the button. The event reaches the bloc and the error is gone.
flutter runConfirmed by raw.githubusercontent.com
Cause 2 of 3
A new route was pushed, and the bloc was provided only to the previous one
1Check
A pushed route is not a child of the widget that provided the bloc, so the new screen cannot look up to it. The flutter_bloc documentation names this as the most common use of BlocProvider.value: handing an existing bloc to a new route.
grep -rn "Navigator\|MaterialPageRoute\|GoRoute" lib/2Fix
Wrap the new screen in BlocProvider.value with the existing instance. It does not close the bloc, which is right because the first screen created it.
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: BlocProvider.of<CounterBloc>(context),
child: const DetailsPage(),
),
),
);3Verify
Open the second screen and read the bloc there with context.read.
flutter runConfirmed by bloclibrary.dev
Cause 3 of 3
The type that is read is not the type that was provided
1Check
The lookup is by type. The message prints the type that was asked for. Compare it letter by letter with the type argument of the provider you expect to answer.
grep -rn "BlocProvider<" lib/2Fix
Make the generic type of the provider and of the read call the same concrete class.
// provide the concrete type
BlocProvider<CounterBloc>(
create: (_) => CounterBloc(),
child: const CounterView(),
)
// read the same type
context.read<CounterBloc>();3Verify
The read succeeds with the exact type in the message.
flutter runConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Provide a bloc as low in the tree as the widgets that use it allow, and read it from contexts below that point.
- Use BlocProvider.value, never create, when you hand an existing bloc to another route.
- Prefer context.read in callbacks and BlocBuilder or context.watch for UI, with the right context in both.
- Keep the generic type arguments of providers and lookups the same concrete class.
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:
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
- DCORAn offline-first field app with three roles, GPS attendance, a dynamic form engine and five languages.
- QwickDrawA live prize-draw platform where hosts run real-time events and participants join from anywhere.
Related fixes
Mobile apps
ProviderNotFoundException: Could not find the correct Provider above this widget
This happens because you used a `BuildContext` that does not include the provider of your choice.
The BuildContext you used does not sit below the provider.
4 causes, each with a fixMobile apps
Flutter says the context does not include a Navigator
The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.
Navigator.of looks upward from the context you pass.
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
Bloc or Cubit throws StateError after it was closed
A StateError is thrown when add is called on a closed bloc
A bloc that was closed cannot be used again.
3 causes, each with a fixMobile 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
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 fix