Looking up a deactivated widget‘s ancestor is unsafe

背景:
showCupertinoModalPopup
中使用Navigator.of(context).pushNamedAndRemoveUntil(RouteName.login, (route) => false);
报错:Looking up a deactivated widget’s ancestor is unsafe

解决方案:先将CupertinoModal关闭,再使用不同的的context去跳转;
注意: _context和context是不同的

Navigator.pop(_context); Future.delayed(Duration(milliseconds: 300), () { Navigator.of(context) .pushNamedAndRemoveUntil(RouteName.login, (Route route) => false); });

void _showAlertDialog(BuildContext context) {
    showCupertinoModalPopup<void>(
      context: context,
      builder: (BuildContext _context) => CupertinoAlertDialog(
        title: const Text('退出后不删除任何历史数据, 请确认是否退出'),
        // content: const Text('Proceed with destructive action?'),
        actions: <CupertinoDialogAction>[
          CupertinoDialogAction(
            /// This parameter indicates this action is the default,
            /// and turns the action's text to bold text.
            isDefaultAction: true,
            onPressed: () {
              Navigator.pop(_context);
            },
            child: const Text('取消'),
          ),
          CupertinoDialogAction(
            /// This parameter indicates the action would perform
            /// a destructive action such as deletion, and turns
            /// the action's text color to red.
            isDestructiveAction: true,
            onPressed: () async {
              // final isLogout = await loginOut();
              // await UserUtil.cleanToKen();
              Navigator.pop(_context);
              Future.delayed(Duration(milliseconds: 300), () {
                Navigator.of(context)
                    .pushNamedAndRemoveUntil(RouteName.login, (Route<dynamic> route) => false);
              });
            },
            child: const Text('退出登录'),
          ),
        ],
      ),
    );
  }

你可能感兴趣的:(#,fullter,flutter,前端)