Home > Software design >  How can you change URL in the web browser without named routes
How can you change URL in the web browser without named routes

Time:01-26

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.

CodePudding user response:

You should use Navigator 2.0.

This is the best package I'd suggest you to use. Its easy to learn and manage. Check out Go_Router

CodePudding user response:

In the same official docs you have provided there is link for the go_router, which is preffered 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', //            
  • Related