颤振/飞镖:查找已停用小部件的祖先是不安全的



我使用了persistent_bottom_nav_bar 5.0.2包,从主页的第一个选项卡页面我导航到更多的页面,最后我使用以下导航器返回到同一选项卡上的此页面

Navigator.of(context).pushAndRemoveUntil(
CupertinoPageRoute(
builder: (BuildContext context) {
return HomePage();
},
),
(_) => false,
);

我导航成功,但当我点击这个标签时,它抛出了这个异常

════════ Exception caught by gesture ═══════════════════════════════════════════
The following assertion was thrown while handling a gesture:
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.

我甚至不知道原因和解决方案,所以任何帮助都将不胜感激。

您正在从堆栈中删除HomePage下面的所有屏幕。

pushAndRemoveUntil顾名思义,会在堆栈中推送一个新屏幕,并从堆栈中删除所有其他屏幕,因此除了当前主页之外,他们没有可用的屏幕。

只使用

if (mounted) {
Navigator.of(context).push(
CupertinoPageRoute(
builder: (BuildContext context) {
return HomePage();
},
),
(_) => false,
);
}

最新更新