通过 Flutter 网址“没有相应的路由”

Mar*_*ark 2 routes flutter flutter-web

我在 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.
Run Code Online (Sandbox Code Playgroud)
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),
      },
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑:我也试过这个onGenerateRoute,但没有运气。

EDIT2:我既在生产URL和本地主机(例如调用这些。http://localhost:12345/#/Page_Two没有。localhost:12345/Page_Two而且localhost:12345/#Page_Two也不能工作。

Edit3:我打电话runAppvoid main() => runApp(new MaterialApp(home: Start()));

cre*_*not 7

这样做的原因是您要Start另一个 MaterialApp.

MaterialApp您返回的第一个小部件将尝试处理传入的 URL。


所以你的结构如下:

-- entrypoint (runApp)
MaterialApp
  Start -- your widget
    MaterialApp
      // the routes live here
Run Code Online (Sandbox Code Playgroud)

第一个MaterialApp没有路由,这会导致错误。

因此,您需要进行的唯一更改是将您的结构转换为:

-- entrypoint (runApp)
Start -- your widget
  MaterialApp
    // the routes live here
Run Code Online (Sandbox Code Playgroud)

代码

将您void main() => runApp(new MaterialApp(home: Start()));的更改为以下内容:

-- entrypoint (runApp)
MaterialApp
  Start -- your widget
    MaterialApp
      // the routes live here
Run Code Online (Sandbox Code Playgroud)