javascript 我正在使用flutter版本3.3.5,对于下面的代码,我收到了这个错误消息“不要跨异步间隙使用BuildContexts

2nc8po8w  于 2023-02-02  发布在  Java
关注(0)|答案(1)|浏览(99)
httpErrorHandle(
        response: res,
        context: context,
        onSuccess: () async {
          SharedPreferences prefs = await SharedPreferences.getInstance();
          Provider.of<UserProvider>(context, listen: false).setUser(res.body);
          await prefs.setString('x-auth-token', jsonDecode(res.body)['token']); 

       
            Navigator.pushNamedAndRemoveUntil(
            context,
            HomeScreen.routeName,
            (route) => false,
          );

        },
      );

我没有找到任何相关的解决方案,此代码使应用程序崩溃

ql3eal8s

ql3eal8s1#

httpErrorHandle(
        response: res,
        context: context,
        onSuccess: () async {
          SharedPreferences prefs = await SharedPreferences.getInstance();
          Provider.of<UserProvider>(context, listen: false).setUser(res.body);
          await prefs.setString('x-auth-token', jsonDecode(res.body)['token']); 

            //if you are using a statefulWidget do:
            if(!mounted) return;

           //if it is a statelessWidget do:
           if(!context.mounted) return;
       
            Navigator.pushNamedAndRemoveUntil(
            context,
            HomeScreen.routeName,
            (route) => false,
          );

        },
      );

相关问题