onGenerateRoute多次调用

问题描述:

我的主要方法是

onGenerateRoute: (route) {
        print(route);
        return Router.generateRoute(route);
      }

如果我使用 https://myurl.com/#/foo/bar 输入URL,则可以看到多次打印.

If I enter URL with https://myurl.com/#/foo/bar I can see printing is called multiple times.

/

/foo

/foo/bar

我有一个问题,因为我想输入 bar 路径,但是却改为输入 foo .

I have an issue because I want to enter bar path, but it gets entered foo instead.

任何想法我应该如何进行?

Any ideas how should I proceed?

情况变得更糟.将initalRoute设置为/home/1/2/3/4/5 ,onGenerateRoute将被调用7次.

It gets worse. Set the initalRoute to /home/1/2/3/4/5 and the onGenerateRoute will be called 7 times.

您可以使用以下方法使它变得不那么可怕:

You can make it less terrible with:

MaterialApp app;

app = MaterialApp(
    onGenerateInitialRoutes: (initialRoute)=>[app.onGenerateRoute(RouteSettings(name:initialRoute))],
    initialRoute: initial,
    onGenerateRoute: app.onGenerateRoute);

这避免了多次调用Route的initState并在路径中的每个'/'调用了一个onGenerateRoute.但是,如果您在浏览器栏中提供路径,仍然会两次调用onGenerateRoute.

This avoid you're Route's initState being called multiple times and one onGenerateRoute per '/' in the path. However, is still calls onGenerateRoute twice if you supply a path in the browser bar.

我已经提交了错误报告: https://github.com/flutter/flutter/issues/71786

I've submitted a bug report: https://github.com/flutter/flutter/issues/71786