You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[go_router] Come up with a better way to get the current location without context #129833
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?
Hi @ghaith-abtah-5
As mentioned in Migration guide, you can work around this by following code (it is not guaranteed to work in later versions, though)
Thanks for reply.
I cannot relay on this method because of "it is not guaranteed to work in later versions", and there was no warning or deprecation annotation that you are going to remove the location property.
As I said, I can try to save the current route and implement this on my own but it will take time and effort.
What is the reason that it was removed without any warning?
there was no warning or deprecation annotation that you are going to remove the location property.
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!
Reacted by coolswood, Memorias de un informático (Lorenzo), NullByte08, Chien, Luca Venir, Lai Zong Hao - Agmo Artisan, Lucas Antchougov, Emir Sürmen, lromanpaletta, Pradeep Peter Murmu and 2 more
Thanks @huycozy for the clarification.
I still didn't get the reason why you have made this change where a lot of developers use the location propriety the old way but maybe they didn't notice the change yet and now we can get it in three lines of code instead of one simple line.
changed the title [-][go_router] Getting the current location without context[/-][+][go_router] Come up with a better way to get the current location without context[/+]on Jul 13, 2023
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.
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 ?
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:
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();
}
}
// in the route list ->
GoRoute(
path: '/:num',
builder: (context, state) {
if (state.matchedLocation != GoRouter.of(context).location) {
return const SizedBox.shrink();
}
context.read<AppBloc>().add(Reload(url: state.pathParameters['num']!)); // api call
return const Renderer();
},
)
Not a GoRouter feature but a workaround I discovered:
window.location isn't updated every time so that mostly shows the last element for me but window.history does get updated on every route.
import 'package:universal_html/html.dart' as html;
final location = html.window.history.state?['state']?['location'];
You will of course need to do a null check with that. ;)
Also that might throw exceptions when building for non-web so make sure to use error handling or good type casts/checks :)
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.
Activity
huycozy commentedon Jul 3, 2023
Hi @ghaith-abtah-5
As mentioned in Migration guide, you can work around this by following code (it is not guaranteed to work in later versions, though)
Please let us know your opinion if it applies to your case or not.
ghaith-abtah-5 commentedon Jul 9, 2023
Thanks for reply.
I cannot relay on this method because of "it is not guaranteed to work in later versions", and there was no warning or deprecation annotation that you are going to remove the location property.
As I said, I can try to save the current route and implement this on my own but it will take time and effort.
What is the reason that it was removed without any warning?
huycozy commentedon Jul 10, 2023
Hi @ghaith-abtah-5,
The Migration guide has been updated after 9.0.2, you can try below sample migration code:
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!
ghaith-abtah-5 commentedon Jul 10, 2023
Thanks @huycozy for the clarification.
I still didn't get the reason why you have made this change where a lot of developers use the location propriety the old way but maybe they didn't notice the change yet and now we can get it in three lines of code instead of one simple line.
chunhtai commentedon Jul 13, 2023
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
[-][go_router] Getting the current location without context[/-][+][go_router] Come up with a better way to get the current location without context[/+]16 remaining items
flutter-triage-bot commentedon May 25, 2024
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.
Tanmay-R-Choudhary commentedon Jul 11, 2024
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)
kevlar700 commentedon Sep 13, 2024
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
iapicca commentedon Sep 13, 2024
I thought the point was NOT to use
contextmErrenst commentedon Oct 22, 2024
Not a GoRouter feature but a workaround I discovered:
window.location isn't updated every time so that mostly shows the last element for me but window.history does get updated on every route.
You will of course need to do a null check with that. ;)
Also that might throw exceptions when building for non-web so make sure to use error handling or good type casts/checks :)
[go_router] add current state getter (#7651)
github-actions commentedon Nov 19, 2024
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.[go_router] add current state getter (flutter#7651)
[go_router] add current state getter (flutter#7651)