启动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',
为什么即使声明了路由也出现上述错误?
答案 0 :(得分:4)
我认为错误日志非常明确。
由于您使用'/'划分了路线,因此将其解释为“子路线”。 实际上,它试图一个接一个地通过这些路线:
* /
* /animals
* /animals/cats
* /animals/cats/lolcats
由于未定义/animals/cats
,因此t出现错误,然后返回初始路径:/
如果您想解决此问题,请使用下划线重新命名您的路线,如下所示:
/animals_cats_lolcats
因此,它将不会尝试获取不存在的/animals/cats