[Solved] Issue after bloc migration - emit was called after an event handler completed normally

Question

Asked by Mr. T on January 02, 2022 (source).

after I migrate to latest version of bloc, I am having an error. Here's my code before migration:

  SignInBloc(
      {required this.authenticationRepository,
      required this.userDataRepository})
      : super(SignInInitialState());

  SignInState get initialState => SignInInitialState();

  @override
  Stream<SignInState> mapEventToState(
    SignInEvent event,
  ) async* {
    if (event is SignInWithGoogle) {
      yield* mapSignInWithGoogleToState();
    }
    if (event is UpdateLastLoginEvent) {
      yield* mapUpdateLastLoginEventToState(event.uid, event.deviceID);
    }
  }

  Stream<SignInState> mapSignInWithGoogleToState() async* {
    yield SignInWithGoogleInProgressState();
    try {
      String res = await authenticationRepository.signInWithGoogle();
      yield SignInWithGoogleCompletedState(res);
    } catch (e) {
      print(e);
      yield SignInWithGoogleFailedState();
    }
  }

  Stream<SignInState> mapUpdateLastLoginEventToState(
      String uid, String deviceID) async* {
    yield UpdateLastLoginInProgressState();
    try {
      String? res = await userDataRepository.lastLogin(uid, deviceID);
      if (res != null) {
        yield UpdateLastLoginCompletedState(res);
      } else {
        yield UpdateLastLoginFailedState();
      }
    } catch (e) {
      print(e);
      yield UpdateLastLoginFailedState();
    }
  }

Here's what I did for the code after migration. Although I am not sure if coding it with the try is still a good thing.

  SignInBloc(
      {required this.authenticationRepository,
      required this.userDataRepository})
      : super(SignInInitialState()) {
    on<SignInEvent>((event, emit) async {
      if (event is SignInWithGoogle) {
        mapSignInWithGoogleToState(emit);
      }
      if (event is UpdateLastLoginEvent) {
        mapUpdateLastLoginEventToState(emit, event.uid, event.deviceID);
      }
    });
  }

  Future<void> mapSignInWithGoogleToState(
    Emitter<SignInState> emit,
  ) async {
    emit(SignInWithGoogleInProgressState());
    try {
      String res = await authenticationRepository.signInWithGoogle();
      emit(SignInWithGoogleCompletedState(res));
    } catch (e) {
      print(e);
      emit(SignInWithGoogleFailedState());
    }
  }

  Future<void> mapUpdateLastLoginEventToState(
    Emitter<SignInState> emit,
    String uid,
    String deviceID,
  ) async {
    emit(UpdateLastLoginInProgressState());
    try {
      String? res = await userDataRepository.lastLogin(uid, deviceID);
      if (res != null) {
        emit(UpdateLastLoginCompletedState(res));
      } else {
        emit(UpdateLastLoginFailedState());
      }
    } catch (e) {
      print(e);
      emit(UpdateLastLoginFailedState());
    }
  }

Here's what I see in the logs but if I try to implement future.whenComplete, it is having a syntax error. Please help. Thanks!

I/flutter (18083): emit was called after an event handler completed normally.
I/flutter (18083): This is usually due to an unawaited future in an event handler.
I/flutter (18083): Please make sure to await all asynchronous operations with event handlers
I/flutter (18083): and use emit.isDone after asynchronous operations before calling emit() to
I/flutter (18083): ensure the event handler has not completed.
I/flutter (18083): 
I/flutter (18083):   **BAD**
I/flutter (18083):   on<Event>((event, emit) {
I/flutter (18083):     future.whenComplete(() => emit(...));
I/flutter (18083):   });
I/flutter (18083): 
I/flutter (18083):   **GOOD**
I/flutter (18083):   on<Event>((event, emit) async {
I/flutter (18083):     await future.whenComplete(() => emit(...));
I/flutter (18083):   });

Answer

Question answered by novas1r1 (source).

Please try to adjust it as follows:

SignInBloc(
      {required this.authenticationRepository,
      required this.userDataRepository})
      : super(SignInInitialState()) {

    // change this
    /*on<SignInEvent>((event, emit) {
      if (event is SignInWithGoogle) {
        mapSignInWithGoogleToState(emit);
      }
      if (event is UpdateLastLoginEvent) {
        mapUpdateLastLoginEventToState(emit, event.uid, event.deviceID);
      }
    });*/

    // to this
    on<SignInWithGoogle>(mapSignInWithGoogleToState);
    on<UpdateLastLoginEvent>(mapUpdateLastLoginEventToState);
  }

And also adjust your functions:

Future<void> mapSignInWithGoogleToState(
    SignInWithGoogle event,
    Emitter<SignInState> emit,
  ) async {
...
}

Future<void> mapUpdateLastLoginEventToState(
    UpdateLastLoginEvent event,
    Emitter<SignInState> emit,
  ) async {
...
}

Using try/catch approach is still fine!

Please let me know if it worked.

BLOC FLUTTER
SHARE: