title: Flutter Bloc Error : emit was called after an event handler completed normally - between two functions
body: <p>I have the following problem...</p>
  <blockquote>
  <p>emit was called after an event handler completed normally. This is
  usually due to an unawaited future in an event handler. Please make
  sure to await all asynchronous operations with event handlers and use
  emit.isDone after asynchronous operations before calling emit() to
  ensure the event handler has not completed.</p>
  <p><strong>BAD</strong>   on((event, emit) {
  future.whenComplete(() =&gt; emit(...));   });</p>
  <p><strong>GOOD</strong>   on((event, emit) async {
  await future.whenComplete(() =&gt; emit(...));   }); )</p>
  </blockquote>
  <p>What happens is that in a function called <strong>_onLogIn</strong>, if the user has changed the language, it goes from there to another function inside the bloc, these two functions do not depend on each other, I mean that each function is called in different pages of the application, but still <strong>_onLogIn</strong> checks the <strong>_onChangeLanguage</strong> function.</p>
  <pre><code>  UserBloc({this.usecases}) : super(UserInitial()) {
      on&lt;LogInEvent&gt;(_onLogIn);
      on&lt;ChangeLanguageEvent&gt;(_onChangeLanguage);
    }
  </code></pre>
  <p><strong>_onLogIn function :</strong></p>
  <pre><code>void _onLogIn(
      LogInEvent event,
      Emitter&lt;StateA&gt; emit,
    ) async {
  
      emit(UserLoading());
  
      final userOrFailure = await services.logIn(
        x: event.x,
        y: event.y,
      );
  
      await userOrFailure.fold((user) async {
  
          /// If the user is logging in for the first time and does not
          /// have a preferred language.
          if (user.preferredLanguage == null) {
            emit(UserSuccess());
  
            emit(UserAlreadyLogged(connectedUser));
  
          } else {
            /// An ChangeLanguageEvent object
            ChangeLanguageEvent event = ChangeLanguageEvent(
              user.preferredLanguage,
              user.someId,
            );
  
            /// Call the other function in the same bloc
            this._onChangeLanguage(
              event,
              emit,
              isFromLogin: true,
            );
          }
        
      }, (failure) {
        emit(UserError(failure.message));
      });
    }
  </code></pre>
  <p><strong>_onChangeLanguage function :</strong></p>
  <pre><code>  void _onChangeLanguage(
      ChangeLanguageEvent event,
      Emitter&lt;StateA&gt; emit, {
      bool isFromLogin = false,
    }) async {
  
      final successOrFailure = await services.updateLanguage(
        event.language,
        event.someId,
      );
  
      await successOrFailure.fold( // ! HERE THE ERROR WHEN I LOG IN; but when i changed the language from the application i don't have an error
        (language) async {
  
          emit(ChangeAppLanguage(language));
  
          final sessionOrFailure = await services.getSession();
  
          sessionOrFailure.fold(
            (session) {
              /// I need this condition to know if the language comes from login
              if (isFromLogin) {
                emit(UserSuccess());
              }
              emit(UserAlreadyLogged(session));
            },
            (failure) =&gt; emit(UserError(failure.message)),
          );
        },
        (failure) {
          emit(UserError(failure.message));
        },
      );
    }
  </code></pre>
  <p>Any idea why? Thank you</p>