Home > Mobile >  How to get previous location of route using Go Router?
How to get previous location of route using Go Router?

Time:10-19

What im trying to do is for example if router came from login to verification screen do something but when prev location from sign up and not login dont do something.

So i want to get the prev location to do some logic to it, im aware that i can pass params to pages then do the logic from it for example passing a bool and do the logic if true or false but i want a better or right way, if you know want i mean. Thank you.

And i encounter this method from go router, what it does? is it for logging purpose only, im trying to add route name or location in the parameter but it throws an error. What param do i need to input in here, i dont know where to find Route thank you.

context.didPush(Route<dynamic> route, Route<dynamic>? previousRoute);

CodePudding user response:

You should pass the route you are coming from to the route you are pushing to. This way you can know where you came from and apply the specific logic.

CodePudding user response:

I found a solution by creating a main route then put the other in the subroute of the router then i will call

context.location

in the page from GoRouter pagckage then it will return the path of the location

 GoRoute(
    name: AppRoute.login,
    path: '/login',
    builder: (context, state) => LoginView(credentials: state.extra as Credentials?),
    routes: [
      GoRoute(
        name: AppRoute.signUp,
        path: 'signup',
        pageBuilder: (BuildContext context, GoRouterState state) => CupertinoPage<void>(
          key: state.pageKey,
          child: const SignupView(),
        ),
      ),
      GoRoute(
        name: AppRoute.verifyNumber,
        path: 'verifyNumber',
        pageBuilder: (BuildContext context, GoRouterState state) => CupertinoPage<void>(
          key: state.pageKey,
          child: VerifyNumberView(user: state.extra as User),
        ),
      ),
     ]
    ),

its important that you put your sub route because if not it will only return its own route not including the base route

result with subroute : /login/forgot_password
result without subroute: /forgot_password

base on the result i can do the logic

  • Related