简体   繁体   English

Flutter “不要跨异步间隙使用 BuildContexts”

[英]Flutter "Do not use BuildContexts across async gaps"

Basically I want to return to my LoginView when the user presses Logout in the dialog.基本上,当用户在对话框中按下 Logout 时,我想返回到我的 LoginView。

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

showLogoutDialog function: showLogoutDialog function:

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

I get this error: "Do not use BuildContexts across async gaps.".我收到此错误:“不要在异步间隙中使用 BuildContexts。”。 Navigator.of(contex) 出错

Anyone who can help me?谁能帮助我?

Thanks in advance!提前致谢!

It is unsafe, try checking if the widget is mounted or not.这是不安全的,请尝试检查小部件是否已安装。

if (mounted) {
  Navigator.of(context);
}

You can find more here你可以在这里找到更多

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

相关问题 不要跨异步间隙使用 BuildContexts flutter - Do not use BuildContexts across async gaps flutter flutter 警告不要跨异步间隙使用 BuildContexts - flutter warning Do not use BuildContexts across async gaps 不要跨异步间隙使用 BuildContexts - Do not use BuildContexts across async gaps 不要跨异步间隙使用 BuildContexts。 将 pub yaml 更新到主要版本后 - Do not use BuildContexts across async gaps. After update pub yaml to the major version 跨异步间隙的 BuildContexts,有什么替代方法? - BuildContexts across Async gaps, what is the alternative to this? 如何解决 Riverpod 的 ConsumerWidget 中的“不要跨异步间隙使用 BuildContext”? - How to solve "Do not use BuildContext across async gaps" in Riverpod's ConsumerWidget? Gridview Flutter - 宽间隙 - Gridview Flutter - wide gaps Flutter:修改 Scaffold 以跟踪当前显示的 Scaffolds 的 BuildContexts 以用于应用程序范围的 SnackBar 方法 - Flutter: Modifying Scaffold to track currently displayed Scaffolds' BuildContexts for app-wide SnackBar methods 在 Flutter 的登录页面中异步/等待执行 CircularProgressIndicator - async/await to do CircularProgressIndicator in a Login Page in Flutter Flutter:异步任务在“initState()”中没有按预期工作 - Flutter: Async task do not work as expected in “initState()”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM