title: Flutter onPressed async function not waiting for await function from cubit/repository
body: <p>I have a signup button which sends an http request to my API. I want to navigate off of the signup page after the request is completed. Using bloc pattern, I add an event to the bloc to navigate to the login page but the signup page isn't waiting for http request to be completed before adding event to bloc.</p>
  <p>Signup Button code :-</p>
  <pre><code>class _SignUpButton extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return BlocBuilder&lt;SignUpCubit, SignUpState&gt;(
        buildWhen: (previous, current) =&gt; previous.status != current.status,
        builder: (context, state) {
          return state.status.isSubmissionInProgress
              ? const CircularProgressIndicator()
              : IconButton(
                  key: const Key('loginForm_continue_raisedButton'),
                  onPressed: state.status.isValidated
                      ? () async {
                        await context.bloc&lt;SignUpCubit&gt;().signUpFormSubmitted();
                        context.bloc&lt;AuthenticationBloc&gt;().add(AuthenticationUserChanged(User.empty));
                      }
                      : () =&gt; print('invalid'),
                  icon: Icon(Icons.keyboard_arrow_right),
                  color: Colors.white,
                  iconSize: 40,
                );
        },
      );
    }
  }
  </code></pre>
  <p>Signup cubit :-</p>
  <pre><code>part 'signup_state.dart';
  
  class SignUpCubit extends Cubit&lt;SignUpState&gt; {
    SignUpCubit(this._authenticationRepository)
        : assert(_authenticationRepository != null),
          super(const SignUpState());
  
    final AuthenticationRepository _authenticationRepository;
  
    //irrelevant code omitted
  
    Future&lt;void&gt; signUpFormSubmitted() async {
      if (!state.status.isValidated) return;
      emit(state.copyWith(status: FormzStatus.submissionInProgress));
      try {
        await _authenticationRepository.signUp(
            name: state.name,
            password: state.password.value,
            mobile: state.mobile.value,
            village: state.village,
            email: state.email.value,
            pincode: state.pincode);
        emit(state.copyWith(status: FormzStatus.submissionSuccess));
      } on Exception {
        emit(state.copyWith(status: FormzStatus.submissionFailure));
      }
    }
  }
  </code></pre>
  <p>Authentication repository :-</p>
  <pre><code>/// Thrown if during the sign up process if a failure occurs.
  class SignUpFailure implements Exception {}
  
  class AuthenticationRepository {
    /// {@macro authentication_repository}
    AuthenticationRepository({
      FirebaseAuth firebaseAuth,
      GoogleSignIn googleSignIn,
    })  : _firebaseAuth = firebaseAuth ?? FirebaseAuth.instance,
          _googleSignIn = googleSignIn ?? GoogleSignIn.standard();
  
    final FirebaseAuth _firebaseAuth;
    final GoogleSignIn _googleSignIn;
  
    /// Stream of [User] which will emit the current user when
    /// the authentication state changes.
    ///
    /// Emits [User.empty] if the user is not authenticated.
    Stream&lt;User&gt; get user {
      return _firebaseAuth.onAuthStateChanged.map((firebaseUser) {
        return firebaseUser == null ? User.empty : firebaseUser.toUser;
      });
    }
  
    //irrelevant code omitted
  
    Future&lt;void&gt; signUp(
        {@required String name,
        @required String password,
        @required String mobile,
        @required String village,
        @required String pincode,
        @required String email}) async {
      assert(
          name != null &amp;&amp; mobile != null &amp;&amp; village != null &amp;&amp; pincode != null);
      try {
        if (email != '' &amp;&amp; password != '') {
          await _firebaseAuth.createUserWithEmailAndPassword(
              email: email, password: password);
        }
        final params = {
          'name': name,
          'phone': mobile,
          'village': village + '_' + pincode,
          'api': 't',
          'login': 'NA'
        };
        Response res = await Dio().get(
            &quot;https://us-central1-greensat-9087a.cloudfunctions.net/addUser&quot;,
            queryParameters: params);
        print(res.data);
      } on Exception {
        throw SignUpFailure();
      }
    }  
  
  }
  </code></pre>
  <p>Authentication bloc :-</p>
  <pre><code>class AuthenticationBloc
      extends Bloc&lt;AuthenticationEvent, AuthenticationState&gt; {
    AuthenticationBloc({
      @required AuthenticationRepository authenticationRepository,
    })  : assert(authenticationRepository != null),
          _authenticationRepository = authenticationRepository,
          super(const AuthenticationState.unknown()) {
      _userSubscription = _authenticationRepository.user.listen(
        (user) =&gt; add(AuthenticationUserChanged(user)),
      );
    }
  
    final AuthenticationRepository _authenticationRepository;
    StreamSubscription&lt;User&gt; _userSubscription;
  
    @override
    Stream&lt;AuthenticationState&gt; mapEventToState(
      AuthenticationEvent event,
    ) async* {
      if (event is AuthenticationUserChanged) {
        yield _mapAuthenticationUserChangedToState(event);
      } else if (event is AuthenticationLogoutRequested) {
        unawaited(_authenticationRepository.logOut());
      } else if (event is AuthenticationOTPRequested) {
        yield _mapAuthenticationUserOTPRequested(event);
      } else if (event is AuthenticationSignupStarted) {
        yield _mapAuthenticationUserSignupStarted(event);
      }
    }
  
    @override
    Future&lt;void&gt; close() {
      _userSubscription?.cancel();
      return super.close();
    }
  
    AuthenticationState _mapAuthenticationUserChangedToState(
      AuthenticationUserChanged event,
    ) {
      print('user changed state');
      print(event.user);
      return event.user != User.empty
          ? AuthenticationState.authenticated(event.user)
          : const AuthenticationState.unauthenticated();
    }
  
    AuthenticationState _mapAuthenticationUserOTPRequested(
      AuthenticationOTPRequested event,
    ) {
      return const AuthenticationState.unauthenticatedOTP();
    }
  
    AuthenticationState _mapAuthenticationUserSignupStarted(
      AuthenticationSignupStarted event,
    ) {
      return const AuthenticationState.signupStarted();
    }
  }
  </code></pre>