gpt4 book ai didi

flutter - "There was no corresponding route"通过 Flutter 网址

转载 作者:行者123 更新时间:2023-12-04 15:58:17 24 4
gpt4 key购买 nike

我在 Flutter Web 应用程序中使用命名路由进行导航。导航到所需的路由时,URL 会更新,但我无法通过 URL 栏直接导航到该路由。每次我尝试在 URL 中添加路径时,它都会将我带到“.../#/”
使用更新的 URL 执行热重新加载时,我收到以下错误:

Could not navigate to initial route.
The requested route name was: "/Page_One"
There was no corresponding route in the app, and therefore the initial route specified will be ignored and "/" will be used instead.
class Start extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My Site',
theme: ThemeData(...),
initialRoute: '/',
routes: <String, WidgetBuilder> {
"/": (context) => MainPage(),
"/Page_One": (context) => Page2(0),
"/Page_Two": (context) => Page2(1),
"/Page_Three": (context) => Page2(2),
},
);
}
}
编辑:我也用 onGenerateRoute 试过这个没有运气。
EDIT2:我在生产 URL 和本地主机上调用这些(例如 http://localhost:12345/#/Page_Two 。不, localhost:12345/Page_Twolocalhost:12345/#Page_Two 也不起作用。
Edit3: 我打电话 runApp来自 void main() => runApp(new MaterialApp(home: Start()));

最佳答案

原因是您要退回您的 Start 中的小部件另一个 MaterialApp .
第一个MaterialApp您返回的小部件将尝试处理传入的 URL。

所以你的结构如下:

-- entrypoint (runApp)
MaterialApp
Start -- your widget
MaterialApp
// the routes live here
第一个 MaterialApp没有路由,这会导致错误。
因此,您需要进行的唯一更改是将您的结构转换为:
-- entrypoint (runApp)
Start -- your widget
MaterialApp
// the routes live here
代码
更改您的 void main() => runApp(new MaterialApp(home: Start()));到以下几点:
void main() => runApp(Start());

关于flutter - "There was no corresponding route"通过 Flutter 网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64236640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com