Bloc or Cubit throws StateError after it was closed
Cannot emit new states after calling closeShort 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.
Fix it fast
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();2Match what you see, then jump to its fix
- 1
The 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
Bad state thrown from add()Your code adds an event to a bloc after calling closeFix - 3
Cannot emit new states after calling closeA Cubit or event handler emits after closeFix
- 1
What does the error look like?
Cannot emit new states after calling closeA 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 create2Fix
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 runConfirmed by pub.dev
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 runConfirmed by pub.dev
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/ | head2Fix
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 runConfirmed by pub.dev
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:
- 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
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
flutter_bloc cannot find my Bloc: BlocProvider.of() called with a context that does not contain it
No ancestor could be found starting from the context that was passed to BlocProvider.of<CounterBloc>().
BlocProvider.of() and context.read() look upward from the context you give them.
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 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 fix