Maaz Zindani

Loading the index…

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

Flutter Bloc throws that emit was called after the event handler completed

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

  • Applies to Flutter 3.x, Dart 3, bloc 8.x, flutter_bloc 8.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

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

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

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

2Fix

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 run

Confirmed by raw.githubusercontent.com

Did this fix it?

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

2Fix

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 run

Confirmed by bloclibrary.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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