7

启动 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',

为什么即使声明了路由,我也会收到上述错误?

4

1 回答 1

5

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

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

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

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

如果您想解决此问题,请使用以下划线重命名您的路线: /animals_cats_lolcats 因此它不会尝试获取/animals/cats不存在的路线

于 2019-02-06T15:25:02.630 回答