title: Flutter: how to wait for entire async method to finish before the next line runs
body: <p>I'm running the script below:</p>
  <pre><code>TextButton(
    onPressed: () async {
      try {
        await BlocProvider.of&lt;RoomCubit&gt;(context).createRoom(test);
        await BlocProvider.of&lt;PlayersCubit&gt;(context) &lt;---- Point 1
        .addPlayer(test.roomId, testPlayer);
        BlocProvider.of&lt;NavigatorCubit&gt;(context) &lt;---- Point 2
                          .moveToLobby(context);
      } catch (e) {}
    },
    child: Text(&quot;Create room&quot;),
  )
  
  class PlayersCubit extends Cubit&lt;PlayerState&gt; {
    PlayersCubit() : super(PlayerInitial());
      
    final PlayerRepository _playerRepository = PlayerRepository();
    Future&lt;void&gt; addPlayer(String roomId, Player player) async {
      emit(PlayerLoading());
      try {
        await _playerRepository.addPlayer(roomId, player);
        List&lt;Player&gt; playerList = await _playerRepository.playerList(roomId);
        emit(PlayerLoaded(player, playerList));
       } 
       catch (e) {}
    }
  }
  </code></pre>
  <pre class="lang-dart prettyprint-override"><code>class NavigatorCubit extends Cubit&lt;NavigatorState&gt; {
    NavigatorCubit() : super(NavigatorInitial());
    late RoomState _roomState;
    late PlayerState _playerState;
  
    void moveToLobby(BuildContext context) {
      print(&quot;$_roomState, $_playerState&quot;);
      switch (_roomState is RoomLoaded &amp;&amp; _playerState is PlayerLoaded) {
        case true:
          {
            Navigator.popAndPushNamed(
              context,
              &quot;/Room&quot;,
            );
          }
          break;
        case false:
          {
            Navigator.pop(context);
          }
          break;
        default:
      }
    }
  
    void getRoomState(RoomState roomState) {
      _roomState = roomState;
    }
  
    void getPlayerState(PlayerState playerState) {
      _playerState = playerState;
    }
  }
  </code></pre>
  <p>What I want it to do is emit PlayerLoaded from the PlayersCubit at the point 1, and then use that state in the NavigatorCubit at Point 2. However, even though I've put await at point 1 - the method at point 2 starts running before a state is emitted at point 1.</p>
  <p>Is there a way I can make the next part wait before the state is emitted at point 1 without setting a timer?</p>