Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.Short answer
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes. If work is still running after that, the later emit fails. Await every async call inside the handler, and use emit.forEach or emit.onEach instead of a stream .listen callback.
Fix it fast
1Find out which cause you have
Open the event handler named in the stack trace and look for any call that returns a Future or Stream and is not awaited, or a callback that calls emit later.
grep -n "emit(" lib/**/*_bloc.dart2Match what you see, then jump to its fix
- 1
The error appears after an API or database call; the handler is async, but a call in it has no await, or uses .then(...).A Future inside the handler is not awaitedFix - 2
The handler returns right after subscribing, and the error shows each time the stream sends a value.A stream is listened to with .listen and emits from the callbackFix
- 1
What does the error look like?
emit was called after an event handler completed normally.
Cause 1 of 2
A Future inside the handler is not awaited
1Check
Find calls like repository.load().then((v) => emit(...)) or a helper that returns a Future without await. The handler's own Future finishes as soon as its last awaited line ends, so a detached call runs afterwards.
grep -n "\.then(" lib/**/*_bloc.dart2Fix
Make the handler async, await the call, and emit afterwards with the result. If the work can outlive the handler, check emit.isDone before emitting.
on<LoadRequested>((event, emit) async {
emit(state.copyWith(loading: true));
final items = await repository.load();
if (emit.isDone) return;
emit(state.copyWith(loading: false, items: items));
});3Verify
Trigger the event again and confirm the state changes and no assertion is printed.
flutter runConfirmed by raw.githubusercontent.com
Cause 2 of 2
A stream is listened to with .listen and emits from the callback
1Check
Search the handler for .listen(...) whose callback calls emit. The handler completes right after subscribing, so every later emit is outside it.
grep -n "\.listen(" lib/**/*_bloc.dart2Fix
Return emit.forEach, which keeps the handler running until the stream ends and emits what onData returns. Use emit.onEach when you only want to run code per item.
on<UserWatched>((event, emit) {
return emit.forEach<User>(
userRepository.user,
onData: (user) => state.copyWith(user: user),
);
});3Verify
Push a value into the stream and watch the UI update with no assertion.
flutter runConfirmed by bloclibrary.dev
How do I stop it coming back?
- Mark every on<Event> handler async when it calls anything that returns a Future, and await each call.
- Never call emit from a callback that can run after the handler returns; use emit.forEach or emit.onEach for streams.
- Check emit.isDone before emitting after a long await.
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 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
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 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