'CupertinoTabView casusing Assertion failed: _history.isNotEmpty is not true on Navigator.pop() (web only)

So I have an app that Pops the current screen once an event happens. The code works fine on iOS and Android. But on the web I get the following error:

Assertion failed:
..\…\widgets\navigator.dart:5083
_history.isNotEmpty
is not true

The debug dialog shows how points the CupertinoTabView of the current tab in the stack. The navigation is to the root page of the Tab view. The tabBuilder:

(context, index) {
        switch (index) {
          case 0:
            return CupertinoTabView(
              navigatorKey: tabOneKey,
              builder: (context) {
                return tabs[index];
              },
            );

            break;
          case 1:
            return CupertinoTabView(
              navigatorKey: tabTwoKey,
              builder: (context) {
                return tabs[index];
              },
            );

            break;
          case 2:
            return CupertinoTabView(
              navigatorKey: tabThreeKey,
              builder: (context) {
                return tabs[index];
              },
            );

            break;
          case 3:
            return CupertinoTabView(
              navigatorKey: tabFourKey,
              builder: (context) {
                return tabs[index];
              },
            );

            break;
          default:
            return Container();
        }
      }

I have tried Wrapping the Navigator.pop() in WidgetsBinding.instance.addPostFrameCallback. The same error happens on web. It changes nothing on mobile. What may be causing this error?



Solution 1:[1]

the navigator work like stack, pop will remove the last page , push will add to the top , this error mean you are trying to remove the last page, but there is none , this can happen if you pop the previous pages after push like the following

 Navigator.of(context).pushNamedAndRemoveUntil( "pageName", (Route<dynamic> route) => false) ;
 Navigator.of(context).pop();

Solution :

first thing to do is don't wrap pop , use this instead if you are not sure if the navigator is empty or not .

Navigator.of(context).maybePop();

and make sure you use the following before any .pop() not pushNamedAndRemoveUntil

Navigator.of(context).pushNamed ("pageName") ;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dxfoso