Is there a way to manually check with go_router (latest version 7.1.1) if a certain path (aka "route") is currently active?
Example: There are 2 paths:
Now I want to check manually if the currently active path is 1) or 2).
Due to the dynamic parameters and the nesting it is not possible to simply compare the currently active path (goRouterState.location)
with the one to be checked, e.g. with contains()
or startsWith()
.
Checking against a regex would be extremely error-prone as well as cumbersome and unnecessary, since go_router can actually handle the matching.
I have not found any functionality of go_router for that.
Since I need this urgently, I am grateful for any tip!
I looked at the code of go_router
and found the function called findMatch
. The function returns a RouteMatchList
It's a method of GoRouteInformationParser
and can be used like this
final router = GoRouter.of(context);
final matches = router.routeInformationParser.configuration.findMatch(router.location).matches;
for (final m in matches.reversed) {
if(m.route is! GoRoute) { // Skip ShellRoute, ...
continue;
}
final route = m.route as GoRoute;
if (route.name == '/projects/:id/details') { // GoRoute.name must be provided
return 2;
}
// ...
}
Hope this helps