简体   繁体   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,
          );

        },
      );

I didn't find any solution related, this code is crashing the application我没有找到任何相关的解决方案,这段代码使应用程序崩溃

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.

相关问题 在这里我正在等待只能在异步 function 错误中使用,但我在 function 中使用异步 - here i am getting await can only use inside async function error but i am using async in my function 为什么我在一行代码中出现 TS(1005) 错误(如下)? - Why am I getting as TS(1005) error on a line of code (below)? 在 React 本机 useEffect 中,使用异步调用我得到一个未定义的,在以下情况下如何避免未定义? - In React native useEffect ,using async call i am getting a undefined , How to avoid that undefined in the below case? 我收到一条错误消息,提示该行不存在 - I am getting an error message for a line that isnt there 为什么我收到此错误消息:(空属性) - Why am i getting this error message am getting: (property of null) 我收到“Object Required”错误消息 - I am getting “Object Required” error message 我收到消息未定义的错误 - I am getting an error that message is not defined 为什么我的代码中出现错误 - Why am I getting a error in my code 我收到 HTML 表单元素未加载到网页中的错误。以下代码的任何解决方案 - i am geeting the error where the HTML form elements are not getting loaded into the webpage .Any solution for the below code 我在下面的代码中遇到未定义的阅读长度错误 - I am facing an undefined reading length error in my code below
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM