Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onGenerateRoute called multiple times

Tags:

routes

flutter

In my main method I have

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

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

/
/foo
/foo/bar

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

Any ideas how should I proceed?

like image 821
Aurimas Deimantas Avatar asked Sep 12 '19 16:09

Aurimas Deimantas


1 Answers

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);

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.

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

like image 156
user48956 Avatar answered Nov 05 '22 14:11

user48956