颤动错误 "Could not navigate to initial route"

Flutter error "Could not navigate to initial route"

启动 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.
════════════════════════════════════════════════════════════════════════════════════════════════════

我已经声明了路线 /animals/cats/lolcats:

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

并将我的 initialRoute 设置为

initialRoute: '/animals/cats/lolcats',

为什么我在声明路由后仍会收到上述错误?

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

由于您使用“/”分隔路线,因此它被解释为 "sub-routes"。 实际上它正在尝试一条接一条地通过这些路线:

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

并且由于 /animals/cats 未定义,t 出现错误然后返回初始路线:/

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