gpt4 book ai didi

flutter "Do not use BuildContexts across async gaps"

转载 作者:行者123 更新时间:2023-12-05 09:01:16 24 4
gpt4 key购买 nike

基本上,当用户在对话框中按下注销时,我想返回到我的登录 View 。

onSelected: (value) async {
switch (value) {
case MenuAction.logout:
final shouldLogout = await showLogOutDialog(context);
final navigator = Navigator.of(context);
if (shouldLogout) {
await FirebaseAuth.instance.signOut();
navigator.pushNamedAndRemoveUntil(
'/login',
(route) => false,
);
}
}
},

显示注销对话框函数:

Future<bool> showLogOutDialog(BuildContext context) {
return showDialog<bool>(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Sign out'),
content: const Text('Are you sure you want to sign out?'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop(false);
},
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
Navigator.of(context).pop(true);
},
child: const Text('Logout'),
),
],
);
},
).then((value) => value ?? false);

我收到此错误:“不要在异步间隙中使用 BuildContexts。”。 Error at Navigator.of(contex

谁能帮帮我?

提前致谢!

最佳答案

这是不安全的,请尝试检查小部件是否未安装,如 Flutter YouTube Channel 所示.

if (!mounted) return
Navigator.of(context);

关于 flutter "Do not use BuildContexts across async gaps",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73592434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com