Flutter error “Could not navigate to initial route”

自闭症网瘾萝莉.ら 提交于 2021-01-27 04:13:49

问题


I'm getting the following error when starting my Flutter app:

══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════
The following message was thrown:
Could not navigate to initial route.
The requested route name was: "/animals/cats/lolcats"
The following routes were therefore attempted:
 * /
 * /animals
 * /animals/cats
 * /animals/cats/lolcats
This resulted in the following objects:
 * MaterialPageRoute<dynamic>("/", animation: null)
 * MaterialPageRoute<dynamic>("/animals", animation: null)
 * null
 * MaterialPageRoute<dynamic>("/animals/cats/lolcats", animation: null)
One or more of those objects was null, and therefore the initial route specified will be ignored and
"/" will be used instead.
════════════════════════════════════════════════════════════════════════════════════════════════════

I have declared the route /animals/cats/lolcats:

'/animals': (context) => AnimalsScreen(context),
'/animals/dogs': (context) => DogsScreen(context),
'/animals/cats/lolcats': (context) => LolcatsScreen(context),

and set my initialRoute to

initialRoute: '/animals/cats/lolcats',

Why am I getting the above error even though the route is declared?


回答1:


I think the error logs are quite explicit.

Since you used '/' to divide your routes it's interpreted as "sub-routes". Actually it is trying to go through these routes one after an other:

* /
* /animals
* /animals/cats
* /animals/cats/lolcats

And since /animals/cats isn't define, t is getting an error and then is coming back to initial route : /

If you want to solve this problem rename your routes with underscore like this : /animals_cats_lolcats So it won't try to get /animals/cats which doesn't exist



来源:https://stackoverflow.com/questions/54556381/flutter-error-could-not-navigate-to-initial-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!