1

How can we use pushReplacment using go-router package

I tried this but didn't work

context.pushReplacement("register"); 

To use the Navigator API with named routes (pushNamed, pushReplacementNamed, or pushNamedAndRemoveUntil), the Navigator must be provided with an onGenerateRoute handler.

Here is my setup

final GoRouter _router = GoRouter(
    debugLogDiagnostics: true,

  routes: <RouteBase>[
    GoRoute(
      path: '/',
      builder: (BuildContext context, GoRouterState state) {
        return const MainPage();
      },
      routes: <RouteBase>[
        GoRoute(
          path: 'login',
          builder: (BuildContext context, GoRouterState state) {
            return const Login();
          },
        ),
        GoRoute(
          path: 'register',
          builder: (BuildContext context, GoRouterState state) {
            return const SignUp();
          },
        ),
        
      ],
    ),
  ],
);

I want to navigate between routes in flutter.

Mohammed Malek
  • 164
  • 1
  • 9

2 Answers2

1

You have to name the GoRouter path and then use pushReplacementNamed.

GoRoute(
          path: 'register',
          name : 'register', // add Name
          builder: (BuildContext context, GoRouterState state) {
            return const SignUp();
          },
),

And use it as

context.pushReplacementNamed("register"); 
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
  • ════════ Exception caught by gesture ═══════════════════════════════════════════ Navigator.onGenerateRoute was null, but the route named "signup" was referenced. – Mohammed Malek Mar 11 '23 at 13:06
  • Try restarting. – krishnaacharyaa Mar 11 '23 at 13:27
  • If you are referencing signup you should add name for singup similar to register as shown in the post – krishnaacharyaa Mar 11 '23 at 13:39
-1

You should put / before route name.

context.pushReplacement("/register");
debug_ing
  • 476
  • 3
  • 13
  • ```════════ Exception caught by gesture ═══════════════════════════════════════════ Navigator.onGenerateRoute was null, but the route named "signup" was referenced. ════════════════════════════════════════════════════════════════════════════════``` i face this error – Mohammed Malek Mar 11 '23 at 07:53
  • Did you add router config to MaterialApp? – debug_ing Mar 11 '23 at 08:10
  • yes i added routerConfig: _router, – Mohammed Malek Mar 11 '23 at 08:15