How to get previous location of route using Go Router?
Asked Answered
Z

2

5

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);
Zanze answered 19/10, 2022 at 7:23 Comment(0)
D
8

You can use routDelegate to list all the routes in the nav stack then you can pick the one before the last

GoRouter.of(context).routerDelegate.currentConfiguration.matches.map((e) => e.matchedLocation)

this will be the output => (/path1, /path2, /path3, /path4) and path4 will be the current screen

Docker answered 10/10, 2023 at 14:0 Comment(0)
D
0

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.

Dream answered 19/10, 2022 at 7:53 Comment(2)
this is my last resort if no more answers is given.Zanze
@KenVerganio were you ever able to pass the route you were coming from? How were you able to get the previous route?Wyman

© 2022 - 2024 — McMap. All rights reserved.