Flutter按顺序删除对话框



我试图删除特定序列中的对话框,但遇到以下错误:

E/flutter (14457): [ERROR:flutter/shell/common/shell.cc(188)] Dart Error: Unhandled exception:
E/flutter (14457): Looking up a deactivated widget's ancestor is unsafe.
E/flutter (14457): 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 inheritFromWidgetOfExactType() in the widget's didChangeDependencies() method.

这里的顺序:

  1. 列表项
  2. 打开"确认"对话框1
  3. 关闭(按下(对话框1
  4. 打开"加载"对话框2
  5. 关闭(Navigator.of(context(.pop(((
  6. 打开带有成功消息的对话框3

对话框1和2都使用Navigator.of(context(.pop((.

我该怎么解决这个问题?

使用函数Navigator.of(context).pop()时,不要使用上一个对话框中的上下文,请尝试使用页面的上下文Looking up a deactivated widget's ancestor is unsafe的原因是上一个对话框已关闭,但您使用该对话框的上下文。你可以看到源代码:

static NavigatorState of(
BuildContext context, {
bool rootNavigator = false,
bool nullOk = false,
}) {
final NavigatorState navigator = rootNavigator
? context.rootAncestorStateOfType(const TypeMatcher<NavigatorState>())
: context.ancestorStateOfType(const TypeMatcher<NavigatorState>());// here check the ancestor state,and throw the error
assert(() {
if (navigator == null && !nullOk) {
throw FlutterError(
'Navigator operation requested with a context that does not include a Navigator.n'
'The context used to push or pop routes from the Navigator must be that of a '
'widget that is a descendant of a Navigator widget.'
);
}
return true;
}());
return navigator;
}

最新更新