Navigator always start with route '/'

lgvaz

The route '/' keeps being automatically pushed to the Navigator on the start of the application.

Let's start with this example of the official docs.

Everything works fine but if you change '/' to '/home' (change made both to initiaRoute and routes) we get the following error:

The requested route name was: "/home"
The following routes were therefore attempted:
* /
* /home
This resulted in the following objects:
* null
* MaterialPageRoute<dynamic>(RouteSettings("/home", null), animation: null)
 One or more of those objects was null, and therefore the initial route specified will be ignored and
"/" will be used instead.

Question 1: I'm explicitly defining '/home' as the initialRoute, why it's insisting to go to '/'?


I tried to fix the error by adding a blank page for '/'.
The error was 'fixed' and the app was starting on the correct page, but the widget tree was looking like this:
widget_tree

Container is the "blank page" I created associated with '/' and still being pushed. For even more confusion it's on the top of the navigation stack!

Question 2: Why FirstScreen is being shown instead of Container?

Derek Lakin

This caught me out, too. If you look at the documentation for the initialRoute property:

If the route contains slashes, then it is treated as a "deep link", and before this route is pushed, the routes leading to this one are pushed also. For example, if the route was /a/b/c, then the app would start with the three routes /a, /a/b, and /a/b/c loaded, in that order.

What the example fails to explain is that / is also pushed. So if your initialRoute is /home then it first pushes /, then it pushes /home.

The (thankfully very simple) fix is to use routes that don't start with /, so in your case, just home :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to start a FlutterActivity and change route

navigator.serviceWorker.controller is always null

React Native Navigator Route Object is Undefined

Angular Route Start and Route End Events

Activated Route URL Always Empty

Using Navigator.popUntil and route without fixed name

Flutter Navigator: Dynamic initial route

Angular - how to land always on the start page when refreshing the browser from a secondary route

How to set always first screen of Stack Navigator inside Tab Navigator React Navigation 5

Can xcode be configured to always display the files navigator?

Why is initial route not the only route on the stack after building Navigator?

Add a route for a screen, not in main Navigator

Javascript navigator.userAgent always show the same agent in Firefox

Start and end on (near) the route in mysql

Add always constraint to route

Route path always rendering

Always route file path to Desktop

Skiasharp Start Coordinates always 0

Use navigator to automatically route in the form flow

Navigator: cannot find a generator/route for pushNamed

Flutter - Push navigator route in build method

navigator.onLine always returning true in electron

how to define route in navigator.pop

Setting variable as navigator route flutter

How to get the current route name of a Stack Navigator that is inside a Bottom Tab Navigator?

route always not found laravel 9

Why is the asterisk route always rendered?

Overriding window.navigator.onLine to always be online

React navigation tab navigator - Always pass the same route params when pressing tab