Flutter:如何在下一行运行之前等待整个异步方法完成

Flutter: how to wait for entire async method to finish before the next line runs

我是运行下面的脚本:

TextButton(
  onPressed: () async {
    try {
      await BlocProvider.of<RoomCubit>(context).createRoom(test);
      await BlocProvider.of<PlayersCubit>(context) <---- Point 1
      .addPlayer(test.roomId, testPlayer);
      BlocProvider.of<NavigatorCubit>(context) <---- Point 2
                        .moveToLobby(context);
    } catch (e) {}
  },
  child: Text("Create room"),
)

class PlayersCubit extends Cubit<PlayerState> {
  PlayersCubit() : super(PlayerInitial());
    
  final PlayerRepository _playerRepository = PlayerRepository();
  Future<void> addPlayer(String roomId, Player player) async {
    emit(PlayerLoading());
    try {
      await _playerRepository.addPlayer(roomId, player);
      List<Player> playerList = await _playerRepository.playerList(roomId);
      emit(PlayerLoaded(player, playerList));
     } 
     catch (e) {}
  }
}
class NavigatorCubit extends Cubit<NavigatorState> {
  NavigatorCubit() : super(NavigatorInitial());
  late RoomState _roomState;
  late PlayerState _playerState;

  void moveToLobby(BuildContext context) {
    print("$_roomState, $_playerState");
    switch (_roomState is RoomLoaded && _playerState is PlayerLoaded) {
      case true:
        {
          Navigator.popAndPushNamed(
            context,
            "/Room",
          );
        }
        break;
      case false:
        {
          Navigator.pop(context);
        }
        break;
      default:
    }
  }

  void getRoomState(RoomState roomState) {
    _roomState = roomState;
  }

  void getPlayerState(PlayerState playerState) {
    _playerState = playerState;
  }
}

我想要它做的是在第 1 点从 PlayersCubit 发出 PlayerLoaded,然后在第 2 点的 NavigatorCubit 中使用该状态。但是,即使我在第 1 点放置了 await - 方法在点 1 发出状态之前,点 2 开始 运行。

有没有办法让下一部分在点 1 发出状态之前等待而不设置计时器?

对我有用的方法非常简单,将 playerState 和 roomState 作为参数传递给 NavigatorCubit 中的 moveToLobby 函数。

似乎异步函数实际上只等待任何包含 await 的步骤,否则所有其他代码行都是 运行 同步的。