【问题标题】:"There was no corresponding route" through Flutter web URL通过 Flutter web URL “没有对应的路由”
【发布时间】:2021-01-21 23:21:12
【问题描述】:

我在 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 和 localhost 上都调用它们(例如http://localhost:12345/#/Page_Two。不,localhost:12345/Page_Twolocalhost:12345/#Page_Two 也不起作用。

Edit3:我打电话给runApp void main() =&gt; runApp(new MaterialApp(home: Start()));

【问题讨论】:

  • 这似乎是针对子路由的,虽然有用但不幸的是并没有解决这个问题。
  • 能否添加网址以及如何称呼runApp
  • 你在应用程序中使用什么方法来改变路由?
  • @creativecreatorormaybenot 我将信息作为编辑添加到主帖子中。 @YouriLieverdink 我正在使用Navigator.pushNamed 在应用程序中导航。那工作正常。当我转到.../#/Page_Two时,我只想能够导航到Page2(1)

标签: flutter routes flutter-web


【解决方案1】:

原因是您将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() =&gt; runApp(new MaterialApp(home: Start())); 更改为以下内容:

void main() => runApp(Start());

【讨论】:

  • 哇,我一直在使用一个非常过时的颤振项目模板,甚至没有注意到冗余。这不仅修复了这个错误,而且还解决了使用此模板的其他项目的一些问题。谢谢!
  • 完美解决方案!我已经在这个错误上工作了很长时间。原来我正在使用未来的构建器来管理 Firebase.initializeApp() 和另一个 MaterialApp 小部件。感谢@creativecreatorormaybenot
  • 谢谢!
【解决方案2】:

这个呢?

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, {p=0}) => Page2(p),
        "/Page_Two": (context, {p=1}) => Page2(p),
        "/Page_Three": (context, {p=2}) => Page2(p),
      },
    );
  }
}

【讨论】:

    猜你喜欢
    • 2021-09-03
    • 1970-01-01
    • 2021-10-03
    • 2021-11-03
    • 2021-03-16
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    相关资源
    最近更新 更多