Maaz Zindani

Loading the index…

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

flutter_bloc cannot find my Bloc: BlocProvider.of() called with a context that does not contain it

ErrorBlocProvider.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.

  • Applies to flutter_bloc (current), Flutter 3.35, Dart 3.9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The 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. 3The 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

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 run

Confirmed by raw.githubusercontent.com

Did this fix it?

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 run

Confirmed by bloclibrary.dev

Did this fix it?

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 run

Confirmed by raw.githubusercontent.com

Did this fix it?

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:

Related fixes

Mobile 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 fix

Next step

Want this handled for good?

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