[英]How do I fix "Do not use BuildContexts accros async gaps" when using Snack Bar and Navigator in flutter
This is my code这是我的代码
_signinRouter(
userId,
BuildContext context, {
phone = false,
email = false,
googleSignUp = false,
userData,
}) async {
NetworkController network = NetworkController();
setState(() {
_isLoading = true;
});
CallResponse response = await network.fetchUserData(
userId,
phone: phone,
email: email,
);
setState(() {
_isLoading = false;
});
if (response.status == 1) {
debugPrint(response.msg.toString());
//IF USER DATA IS FOUND
showSnackBar('User account exists', context);
} else {
//Since network returns one or zero, 1 for the success of request and 0 for both server error and failure of request
//Response msg will return null if user was not found and it will return an actual server failure msg if it was a server error
if (response.msg == 'null') {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => (googleSignUp)
? GooglePostSignUp(userData: userData)
: ServerSignUp(phoneNumber: userId, userData: userData),
),
);
} else {
showSnackBar(response.msg, context);
}
}
}
}
I am calling the function within a stateful widget and I defined a function to help in calling the snack bar, my app relies heavily on snack bars and I have this same error all across my files.我在一个有状态的小部件中调用 function,我定义了一个 function 来帮助调用小吃店,我的应用程序严重依赖小吃店,我的所有文件都有同样的错误。 This is the snack bar function
这是小吃店 function
void showSnackBar(String? value, BuildContext context) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
value!,
style: p2(
false,
),
),
duration: const Duration(seconds: 2),
action: SnackBarAction(
label: 'Close',
textColor: themeColor,
onPressed: () {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
},
),
),
);
}
I can call the my custom snack bar from anywhere with in my project easily.我可以在我的项目中轻松地从任何地方调用我的自定义小吃店。
I can trick the IDE and lint by removing the Buildcontext type in the function but it not practical, i can check if the widget is mounted before calling the snackbar but i dont think it is very practical???我可以通过删除 function 中的 Buildcontext 类型来欺骗 IDE 和 lint,但它不实用,我可以在调用 snackbar 之前检查小部件是否已安装,但我认为它不太实用???
the simplest way is just put最简单的方法就是放
if (!mounted) return;
for void functions before you call snackbar or anything that requires context.在您调用 snackbar 或任何需要上下文的内容之前获取 void 函数。
I know you are thinking of app performance in mind, Oh: wait.我知道您在考虑应用程序性能,哦:等等。 flutter already did that for you.), You don't need to work around it except if you have time and energy.
flutter 已经为您完成了。),除非您有时间和精力,否则您无需解决它。 Note that, the context has to be passed every time and so checking the context mount every time is not "i don't think it is very practical" situation
请注意,每次都必须传递上下文,因此每次都检查上下文挂载并不是“我认为这不太实用”的情况
Things like this I call (The warning error) .像这样的事情我称之为(The warning error) 。 When working with
async
functions within a context that is likely to be lost due to nesting.在可能因嵌套而丢失的上下文中使用
async
函数时。 You need to handle it with care.你需要小心处理它。
One way to do that is by checking for the widget-mounted state. basically, Don't use context
after async if you're not sure your widget is mounted.一种方法是检查挂载的小部件 state。基本上,如果不确定小部件是否已挂载,请不要在异步后使用
context
。
To be sure use the following:为确保使用以下内容:
if (!mounted) return; // check before calling `Navigator`
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => (googleSignUp)
? GooglePostSignUp(userData: userData)
: ServerSignUp(phoneNumber: userId, userData: userData),
),
);
To make the code short-hand just do the normals:!!要使代码简写,只需执行法线即可:!! :) break the code down in the same file!!
:) 在同一个文件中分解代码!!
Bye.再见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.