Page url not changing in flutter, but the page content changes fine

Viewed 2682

Hi I am trying to implement a page in flutter web that takes an id from the url and shows data according to that id. It's working in the sense that the page gets the data, but the url itself is not changing in the browser.

For the routes section I have put this

                      onGenerateRoute: (settings) {
                        List<String> routeSplit = settings.name.split('/');
                        if (routeSplit[1] == 'pdp' && routeSplit.length > 2) {
                          int pdpId = int.parse(routeSplit[2]);
                          return MaterialPageRoute(
                              builder: (context) =>
                                  ActivityDescriptionPage(id: pdpId));
                        }
                      },

From this the page gets the id. And to send to the page from the code I use this:

onTap: () => Navigator.pushNamed(context, '/pdp/' + activityId.toString())

So when i execute the above line from say /products page, the new page content is loaded but the url in the browser remains /products only. Am I doing something wrong here?

3 Answers

Just add settings: settings, to MaterialPageRoute

Pass the settings in MaterialPageRoute. Like this,

onGenerateRoute: (settings) {
  List<String> routeSplit = settings.name.split('/');
  if (routeSplit[1] == 'pdp' && routeSplit.length > 2) {
    int pdpId = int.parse(routeSplit[2]);
    return MaterialPageRoute(
      settings: settings,
      builder: (context) => ActivityDescriptionPage(id: pdpId)
    );
  }
},

It will change the page content & the URL as well.

Related