flutter/dart: Looking up a deactivated widget's ancestor is unsafe

Viewed 28

I have used persistent_bottom_nav_bar 5.0.2 package and from the first tab page which is homepage I navigate to further some pages and than finally I use the following navigator to come back to this page on same tab

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

I Navigate successfully but when I click on this tab it throw this exception

════════ 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.

I even don't know the reason as well as solution so any help will be appreciated.

1 Answers

You are removing all the screens below HomePage from the stack.

pushAndRemoveUntil as name suggests pushes a new Screen in stack and removes all the other screens from stack as a result theirs no screen available to pop except the current HomePage.

Instead use only

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