Navigate to another screen when api response success in flutter with provider

44 Views Asked by At

After a while searching on stackoverflow, there is no answer that exactly what I need. Example like: At the Login screen, after press 'login' button, I will call post api, then wait the response and check if it true will navigate to HomeScreen. But the current way I am using now always get 'Don't use 'BuildContext's across async gaps' So what is the right way to use with provider.

      void login({
    required BuildContext context,
    required String userName,
    required String password,
  }) async {
    isLoading = true;
    notifyListeners();

    final loginResult = await DoLoginApi(DanhMucNguoiSuDungLoginRequest(
      Ma: userName,
      MatKhau: password,
    ));
    if (loginResult.Status == '0') {
      setCurrentUser(loginResult);
      isLoading = false;
      notifyListeners();
      Navigator.of(context)
          .push(MaterialPageRoute(builder: (_) => HomeScreen()));
    } else {
      loginMsg = loginResult.ErrorMsg;
      isLoading = false;
      notifyListeners();
    }
  }
0

There are 0 best solutions below