繁体   English   中英

我正在使用 flutter 版本 3.3.5。 并且我收到此错误消息“不要在异步间隙中使用 BuildContexts。” 对于下面的代码

[英]I am using flutter version 3.3.5. and I am getting this error message "Do not use BuildContexts across async gaps." for the code below

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,
          );

        },
      );

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

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,
          );

        },
      );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM