Home > Mobile >  Do not use BuildContexts across async gaps. After update pub yaml to the major version
Do not use BuildContexts across async gaps. After update pub yaml to the major version

Time:06-05

I have upgraded the pub yaml to the major version flutter pub upgrade --major versions and it gave me a lot of suggestions error I don’t understand why?. Can someone explain?

And this is for an example. It says Do not use BuildContexts across async gaps What am I suppose to do here.

_resetEmail(String password,) async {
    final user = FirebaseAuth.instance.currentUser;
    final credential =
        EmailAuthProvider.credential(email: user!.email!, password: password);
    try {
      UserCredential;
      await FirebaseAuth.instance.currentUser
          ?.reauthenticateWithCredential(credential);

       ///The problem is here
      Navigator.push(
          context,
          PageTransition(
              type: PageTransitionType.rightToLeft,
              child: const ResetEmailScreen()));
        ///

    } on FirebaseAuthException {
      Fluttertoast.showToast(
        msg: 'Wrong password',
        gravity: ToastGravity.TOP,
        toastLength: Toast.LENGTH_LONG,
        backgroundColor: Colors.grey[400],
        textColor: Colors.black,
      );
    }
  }

CodePudding user response:

Before Navigator.push add a condition if (mounted). You are using a context in an async method. While this method is being executed the context can change. But this context is being passed to the navigator. Hence the error i think..

  • Related