Когда вы используете функцию 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;
}