Maaz Zindani

Loading the index…

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

Bloc or Cubit throws StateError after it was closed

ErrorCannot emit new states after calling close

Short answer

A bloc that was closed cannot be used again. add and emit throw StateError on a closed instance. Find who called close: BlocProvider closes what it created, so do not share that instance elsewhere. Use BlocProvider.value for existing blocs and check isClosed before emitting from async code.

  • Applies to Dart 3.9, Flutter 3.35, bloc 9, flutter_bloc 9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log when your bloc closes, with a BlocObserver, so you can see which screen closed it before the failing call.

    class LogObserver extends BlocObserver {
      @override
      void onClose(BlocBase bloc) {
        print('closed: ${bloc.runtimeType}');
        super.onClose(bloc);
      }
    }
    // in main(): Bloc.observer = LogObserver();
  2. 2Match what you see, then jump to its fix

    1. 1The error appears after leaving a screen and coming back, or on another screen using the same instanceBlocProvider closed a bloc you still use elsewhereFix
    2. 2Bad state thrown from add()Your code adds an event to a bloc after calling closeFix
    3. 3Cannot emit new states after calling closeA Cubit or event handler emits after closeFix

What does the error look like?

  • Cannot emit new states after calling close
  • A StateError is thrown when add is called on a closed bloc

Cause 1 of 3

BlocProvider closed a bloc you still use elsewhere

1Check

BlocProvider with create closes the instance for you when it leaves the tree. If the same bloc object is handed to other places, the next use hits a closed bloc.

grep -rn "BlocProvider(" lib/ | grep create

2Fix

Create the bloc once where it should live, and give existing instances to new subtrees with BlocProvider.value. Never create a bloc inside .value, because .value does not close it.

BlocProvider.value(
  value: existingBloc,
  child: const DetailPage(),
)

3Verify

Navigate away and back, then trigger the event again.

flutter run

Confirmed by pub.dev

Did this fix it?

Cause 2 of 3

Your code adds an event to a bloc after calling close

1Check

The Bloc.add docs say a StateError is thrown if the bloc is closed, and the event is not processed. Look for callbacks, timers or streams that outlive the widget.

grep -rn "\.close()" lib/

2Fix

Stop whatever still calls add when you close: cancel the stream subscription or timer first, and guard late calls.

if (!bloc.isClosed) {
  bloc.add(RefreshRequested());
}

3Verify

Close the screen while a request is running and confirm no exception reaches the console.

flutter run

Confirmed by pub.dev

Did this fix it?

Cause 3 of 3

A Cubit or event handler emits after close

1Check

BlocBase.emit throws a StateError if the bloc is closed. An awaited call finishing after the screen is gone is the usual path.

grep -rn "emit(" lib/ | head

2Fix

Check isClosed after each await before emitting.

Future<void> load() async {
  final data = await repository.fetch();
  if (isClosed) return;
  emit(LoadedState(data));
}

3Verify

Leave the page during the request; nothing should throw.

flutter run

Confirmed by pub.dev

Did this fix it?

How do I stop it coming back?

  • Let BlocProvider own the lifetime of a bloc and use BlocProvider.value only to pass existing ones down.
  • Cancel timers and stream subscriptions in close().
  • Check isClosed after every await in a Cubit.

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.