Home > Mobile >  Calling Navigator.of(context, rootNavigator: true).pop() gives black screen
Calling Navigator.of(context, rootNavigator: true).pop() gives black screen

Time:01-25

I have a open Chrome custom tab and then I open another app. This other app then opens my app via Asset Links and somehow my navigation stack is not preserved correctly. This means that a new page/widget is placed on top of my custom tab and I need the user to land on the custom tab again (I have another issue on this also, but now I just want to try a dirty fix).

So I want to just close that added page/widget. If I manually navigate back on my Android device via the back arrow, it works great and the added page/widget is closed and I see my custom tab.

I have tried calling

Navigator.of(context, rootNavigator: true).pop();

If I call "pop" once I get a black page and still if I then manually navigate back I come back to the custom tab. If I pop 2 times like this

Navigator.of(context, rootNavigator: true)..pop()..pop();

I get

The following assertion was thrown building Navigator-[LabeledGlobalKey#ad310](dirty, dependencies: [HeroControllerScope, UnmanagedRestorationScope], state: NavigatorState#e4365(tickers: tracking 1 ticker)):
'package:flutter/src/widgets/navigator.dart': Failed assertion: line 5203 pos 12: '!_debugLocked': is not true.

Still I can manually navigate back to the custom tab... Any ideas on mow I can close this widget on top and get back to my custom tab?

CodePudding user response:

you can try using the Navigator.of(context, rootNavigator: true).popUntil() method to pop all the pages/widgets that were added to the navigation stack after your custom tab.

For example, you can use the following code to pop all pages/widgets until your custom tab is reached:

Navigator.of(context, rootNavigator: true).popUntil((route) => route.isFirst);

This method will remove all routes from the navigation stack until it reaches the first route. You can also use a custom predicate to specify the exact route you want to pop to.

Also, using

Navigator.of(context, rootNavigator: true).popUntil((route) => route.settings.name == '/customtab');

This will pop all the routes until it reaches the route with name '/customtab'.

It's important to note that you should only use the Navigator.of(context, rootNavigator: true) when you are sure that the context parameter is the root context. If you are not sure, you should use Navigator.of(context) instead.

CodePudding user response:

You have to remove rootNavigator: true which is causing you to pop from the first instance in your navigator. The navigator is like a list of pages that you opened in the order you opened them. That attribute is forcing all routing from the first page on the navigator, so when you pop, you remove everything and remain with no screen at all. Try Navigator.of(context).pop();

  • Related