Flutter 无法从抽屉中关闭alertDialog
单击抽屉上的元素时,我尝试显示对话框:Flutter 无法从抽屉中关闭alertDialog,flutter,dart,Flutter,Dart,单击抽屉上的元素时,我尝试显示对话框: ListTile( title: Text('Home'), leading: Icon(Icons.home_outlined), onTap: () { Navigator.pop(context); showDialog( context: context, child: AlertDialog( title
ListTile(
title: Text('Home'),
leading: Icon(Icons.home_outlined),
onTap: () {
Navigator.pop(context);
showDialog(
context: context,
child: AlertDialog(
title: Text('Confirm Sign out'),
content: Text('Are sure to sign out from app now?'),
actions: [
FlatButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Cancel'),
),
],
));
},
),
抽屉小部件的完整代码:
但是,dilaog没有撤销,并且显示以下错误:
Looking up a deactivated widget's ancestor is unsafe.
At this point the state of the widget's element tree is no longer stable.
To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.
When the exception was thrown, this was the stack:
#0 Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure> (package:flutter/src/widgets/framework.dart:3906:9)
#1 Element._debugCheckStateIsActiveForAncestorLookup (package:flutter/src/widgets/framework.dart:3920:6)
#2 Element.findAncestorStateOfType (package:flutter/src/widgets/framework.dart:4039:12)
#3 Navigator.of (package:flutter/src/widgets/navigator.dart:2218:40)
#4 Navigator.pop (package:flutter/src/widgets/navigator.dart:2107:15)
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#016ce
debugOwner: GestureDetector
state: possible
won arena
finalPosition: Offset(140.5, 462.5)
finalLocalPosition: Offset(9.5, 34.0)
button: 1
sent tap down
从onTap中删除Navigator.popcontext
为什么它不起作用
因为这个当前小部件从小部件树中弹出,所以在那个之后,当前小部件的上下文也将不可用
onTap: () {
Navigator.pop(context); // Remove this to pop from here
// Rest code
}
从onTap中删除Navigator.popcontext
为什么它不起作用
因为这个当前小部件从小部件树中弹出,所以在那个之后,当前小部件的上下文也将不可用
onTap: () {
Navigator.pop(context); // Remove this to pop from here
// Rest code
}
这对我有用
这对我很有用我在取消对话框时没有收到任何异常,你能显示更多的代码,比如你正在使用的上下文吗?我刚刚编辑了代码我在取消对话框时没有收到任何异常,你能显示更多的代码,比如你正在使用的上下文吗?我刚刚编辑了代码
Navigator.of(context,rootNavigator: true).pop();