Search code examples
flutterdartnavigator

Navigator.popUntil flutter show black screen


I have two different flows in my project.

  1. Main Page -> PageA -> Page B -> Page C -> Page A

From Page C to Page A, I use this code , which works fine.

   Navigator.popUntil(context, ModalRoute.withName(AssetMenu.ROUTE));
  1. When receive push notification, user tap on the push notification, I want it straight away navigate like this

Main Page -> Page B -> Page C -> then back to Page A when button in Page C is clicked.

But when button in Page C is clicked, it shows black screen.

How should I handle this?


Solution

  • In your second use case Page A is missing. When you invoke Navigator.popUntil() the navigator goes up the stack of routes and pops them until it find the specified one. If the specified route is not on the stack, it will remove routes until the stack is empty. That is why you get the black screen.

    If you want to turn Main Page -> Page B -> Page C into Main Page -> Page A you have to invoke Navigator.pushAndRemoveUntil() where the new route is Page A and the predicate matches the Main Page. Alternatively use Navigator.pushNamedAndRemoveUntil().