0

Since "named routes are no longer recommended for most applications" how can you change the URL in web browser when you push a new route onto Navigator stack?

E.g. URL is http://localhost:37291/#/ and after performing

Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => const SecondRoute()),
  );

I would want it to change to http://localhost:37291/#/secondRoute.

mip
  • 8,355
  • 6
  • 53
  • 72

1 Answers1

0

In the same official docs you have provided there is link for the go_router, which is preferred by flutter team

You can use the path property of GoRoute to achieve the desired url


Initial setup:

// GoRouter configuration
final _router = GoRouter(
  routes: [
    GoRoute(
      path: '/',
      builder: (context, state) => HomeScreen(),
    ),
    GoRoute(
      path: '/secondRoute', //  here you can add the path
      builder: (context, state) => SecondScreen(), // This is the desired screen you would want to go to 
    ),
  ],
);

Now your url when routing to SecondScreen would be http://localhost:37291/#/secondRoute

If you want to have the very same functionality you had with Navigator stack you need to implement stuff around Router class. Find out more in this article. You could possibly refer this article for the same.


Further reference:

Flutter: go_router how to pass multiple parameters to other screen?

mip
  • 8,355
  • 6
  • 53
  • 72
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
  • Yep, been checking it, but it seems with `go_router` you can't do various things (including pushing named route as in question), it's very limitted. I think now you have to implement all the stuff around `Router` class. – mip Jan 26 '23 at 14:21
  • It is now under flutter's care, as it is officially stated in the docs to use `go_router` and there are lot many updates which are seen in the `go_router` so yeah,I agree, we have to implement all the stuff around `GoRouter` class – krishnaacharyaa Jan 26 '23 at 14:24
  • More like `Router` class if we want to have the same functionality we had before with `Navigator` stack. – mip Jan 26 '23 at 14:24
  • yes , indeed !!! – krishnaacharyaa Jan 26 '23 at 14:25
  • Good article: https://medium.com/flutter/learning-flutters-new-navigation-and-routing-system-7c9068155ade Will need to go through all of this.... – mip Jan 26 '23 at 14:26
  • You could even refer these https://dev.to/codemagicio/a-beginners-guide-to-gorouter-in-flutter-24ec and https://blog.devgenius.io/guide-to-go-router-flutter-1ddcfa56d39c these are of great help if you are using `go_router` hope it helps! – krishnaacharyaa Jan 26 '23 at 14:29
  • Could i possibly draw your attention to this post https://blog.codemagic.io/flutter-navigator2/, this is recent post, the post you suggested seems to be 2 years old. And lot of things have changed in this span – krishnaacharyaa Jan 26 '23 at 14:33
  • 1
    I have updated my post for the completeness with the above mentioned code. Hope it helps – krishnaacharyaa Jan 26 '23 at 14:34