-
Notifications
You must be signed in to change notification settings - Fork 27.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[go_router] Come up with a better way to get the current location without context #129833
Comments
Hi @ghaith-abtah-5 GoRouter _router;
final String location = _router.routerDelegate.currentConfiguration.uri.toString();
final bool canPop = _router.routerDelegate.canPop; Please let us know your opinion if it applies to your case or not. |
Thanks for reply. |
Hi @ghaith-abtah-5, final RouteMatch lastMatch = _router.routerDelegate.currentConfiguration.last;
final RouteMatchList matchList = lastMatch is ImperativeRouteMatch ? lastMatch.matches : _router.routerDelegate.currentConfiguration;
final String location = matchList.uri.toString();
Even though this is announced in the Changelog (this is a breaking change since 9.0.0), it should be warned in the API documentation. Thanks! |
Thanks @huycozy for the clarification. |
The reason we remove the location is to avoid people looking up GoRouter.of(context).location since it will not rebuild the widget if it changes. but i agree the current workaround is ugly |
Here is a crazy idea. How about we have a method named 'location' that implements this workaround, then we don't have any breaking changes. Once we have a proper alternative solution then remove this location method. |
Inspired by @chrisdlangham 's idea I have created this: extension GoRouterExtension on GoRouter {
String location() {
final RouteMatch lastMatch = routerDelegate.currentConfiguration.last;
final RouteMatchList matchList = lastMatch is ImperativeRouteMatch ? lastMatch.matches : routerDelegate.currentConfiguration;
final String location = matchList.uri.toString();
return location;
}
} for anyone else to use it. Just simply use it like this: GoRouter.of(context).location() |
make it a property and you are golden: extension GoRouterLocation on GoRouter {
String get location {
final RouteMatch lastMatch = routerDelegate.currentConfiguration.last;
final RouteMatchList matchList = lastMatch is ImperativeRouteMatch
? lastMatch.matches
: routerDelegate.currentConfiguration;
return matchList.uri.toString();
}
} |
Can I somehow get the access to parameters sent to current route? |
alright I will just add a getter to get routeMatchList in GoRouter. Though this is not critical as there is work around, but it is getting quite annoying when developing an app |
there is problem with the above approach as it seems the getter, /// The last matching route.
RouteMatch get last => matches.last; getter Also, this issue very likely seems to cause the issue, #116855 will shared my code, which actually preventing that from happening issue... |
is it me or when I use this new way to get current location, it shows instead the previous location and not the current one ?... Is anyone having the same issue ? |
Nope nope, actually it does current location. |
Yes, if I use Possibly related: #132930 |
This issue is assigned to @chunhtai but has had no recent status updates. Please consider unassigning this issue if it is not going to be addressed in the near future. This allows people to have a clearer picture of what work is actually planned. Thanks! |
This issue was assigned to @chunhtai but has had no status updates in a long time. To remove any ambiguity about whether the issue is being worked on, the assignee was removed. |
For me, it is rebuilding all the routes that were previously injected in the stack (im using context.push to navigate). This might just be me though, as all the pages in my project have the same path: /:param1/:param2 I fetch additional data, via api call, based on the params and show content according to the response on the page. If I use context.go, everything works fine. The reason I need to use context.push is to support back button presses on mobile. I found a related issue, that was seemingly, resolved in previous versions. I dont know if its the same bug. I solved the issue by adding the following code (which I found in a comment on the previous issue I mentioned): Minimum code:
Here is the link to the comment: #123570 (comment) |
I was happy with Vrouter but it's dependencies not being updated in two years just broke my app. Vrouters ability to get the current url was nicer tbh
|
I thought the point was NOT to use |
Is there an existing issue for this?
Use case
After the last update go_router 9.0.0, you have removed the location parameter from GoRouter and cannot be accessed any more without a context.
I cannot anymore use router.location to determine where am I in the application and there are certain scenarios where I cannot provide a context to GoRouterState.of(context).location, deep link or firebase messages that will pop or push some other routes from or to the navigation stack depending on the current location and state.
It was the easiest way to get the location and I can implement a way to do this on my own but it will take so much time and effort to get to the go_router package level.
Proposal
What was the reason to remove the location parameter and is there any chance that it will be back without a context in future versions?
The text was updated successfully, but these errors were encountered: