颤振错误“无法导航到初始路线”

Mag*_*s W 5 dart flutter flutter-navigation

启动Flutter应用程序时出现以下错误:

??? 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.
????????????????????????????????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

我已经宣布路线/animals/cats/lolcats

'/animals': (context) => AnimalsScreen(context),
'/animals/dogs': (context) => DogsScreen(context),
'/animals/cats/lolcats': (context) => LolcatsScreen(context),
Run Code Online (Sandbox Code Playgroud)

并将我设置initialRoute

initialRoute: '/animals/cats/lolcats',
Run Code Online (Sandbox Code Playgroud)

即使声明了路由,为什么仍然出现上述错误?

Fer*_*rdi 5

我认为错误日志非常明确。

由于您使用“/”来划分路线,因此它被解释为“子路线”。实际上,它正试图一条接一条地通过这些路线:

* /
* /animals
* /animals/cats
* /animals/cats/lolcats
Run Code Online (Sandbox Code Playgroud)

由于/animals/cats未定义,t 收到错误,然后返回初始路线:/

如果你想解决这个问题,像这样用下划线重命名你的路线: /animals_cats_lolcats 所以它不会尝试获取/animals/cats不存在的