本文主要是介绍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<dynamic> 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('退出登录'),),],),);}
这篇关于Looking up a deactivated widget‘s ancestor is unsafe的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!