我正在使用颤振版本 3.3.5。我收到以下代码"Do not use BuildContexts across async gaps."的错误消息


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

相关内容

  • 没有找到相关文章

最新更新