Avoiding routing when the destination == current route
I have a menu, and in that I have an onTap handler that should navigate to a route with a parameter that I calculate. Its possible that the user is already on the intended route, and I don't want it to navigate if already on the target route. Is there a nice and reliable way of detecting and preventing navigation to the same as the current route, including query parameters? It would need to cope with any parameters being in the wrong order if it used urls.
I'm currently doing this, but its ugly and error prone :
var samePath = (router.currentConfiguration!.path==AppRoutes.PATH1) && (router.currentConfiguration!.queryParameters['id']==id);
if (!samePath) {
router.push(AppRoutes.PATH1,queryParameters: {'id': id});
}
Hey, great question - and no, not really.
You can check if RouteData objects equal each other, but that won't be the case if query parameters are in a different order. I'm not sure changing that is a good idea, but it's possible.
How about a routeData1.isEquivalentTo(routeData2) which also checks query parameters?
isEquivalentTo would be a decent solution, thanks