1

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?

Mudassir
  • 725
  • 6
  • 28

1 Answers1

3

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") ;
dxfoso
  • 43
  • 1
  • 7
  • I never went around to implementing this solution. If anybody who comes across this answer and implement it, comment with my tag. I will mark it as the answer – Mudassir Jan 19 '23 at 08:29