BlocProvider.of() called with wrong context

BlocProvider.of() called with a context that does not contain a Bloc of type MyBloc.

Quick Answer

The context used to find the Bloc does not have a BlocProvider ancestor for that Bloc type.

Why This Happens

In Flutter with the BLoC pattern, BlocProvider.of(context) looks for a BlocProvider ancestor that provides the requested Bloc type. If no matching BlocProvider exists above the widget, this error occurs. Ensure BlocProvider is placed above the consuming widget.

The Problem

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final bloc = BlocProvider.of<MyBloc>(context); // No BlocProvider!
    return Text('Hello');
  }
}

The Fix

BlocProvider(
  create: (_) => MyBloc(),
  child: MyPage(),
)

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final bloc = BlocProvider.of<MyBloc>(context);
    return Text('Hello');
  }
}

Step-by-Step Fix

  1. 1

    Identify the error

    Look at the error to see which Bloc type was not found in the widget tree.

  2. 2

    Find the cause

    Check if a BlocProvider for the required Bloc type exists above the widget that calls BlocProvider.of.

  3. 3

    Apply the fix

    Add a BlocProvider for the required Bloc type above the consuming widget in the widget tree.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free