Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go Router (Flutter)- push the exact same route and refresh that route

With GoRouter, Is there a way to push the exact same route with a different parameter and make the page reload? I am thinking of something like Navigator.pushReplacement

I am using context.go('/my_page/?param={someID}') to push to MyPage (a stateful widget)

The initial push to this route works fine and I load up the page for the particular ID

I am trying to push this same route again (replace the route and reload with different ID) using context.go('/my_page/?param={differentID}'). My breakpoints are hitting the return statement in my GoRoute pageBuilder, and I also hit a breakpoint in MyPage.build. I see the new ID passed into this widget when breakpointing in the build.

But the Page does not rebuild visually (or break in initState - side note, init state is used to load up a couple cubits with the passed in ID - could the page being a stateful widget be the problem?)

Maintain state on the Material Page is false. Also, pushing different routes works just fine.

Is this a stateful widget issue (meaning relocate all of my cubit init calls)? or is there a different way to push the same route?

EDIT _____________

This question was specific to rebuilding the same route, but the greater problem I was working on was to infinitely drill into the same page over and over again, while maintaining the navigation stack.

I was using a state manager to hold a list of routes and trigger the navigation by using context.go() to replace the current route completely.

is there a way to dynamically nest routes with context.push() while maintaining the entire nav stack?

like image 229
Peter Irving Avatar asked Sep 13 '25 17:09

Peter Irving


2 Answers

After some research, I have noticed that you can redirect to the same page by using pageKey: UniqueKey() in the definition of the GoRoute().

Example code from my project:

GoRoute(
    path: RoutePaths.serviceDetail.value,
    name: RouteNames.serviceDetail.value,
    pageBuilder: (context, state) {
        return CustomTransitionPage<void>(
            key: UniqueKey(),
            child: const ServiceDetailPage(),
            transitionsBuilder: (context, animation, secondaryAnimation, child) =>
            PageTransitions.subpageTransition(animation, child),
        );
    },
),
like image 115
Pablo Insua Avatar answered Sep 15 '25 07:09

Pablo Insua


I think context.replace would accomplish what you are trying to do, since it should replace what is currently on the stack.

like image 29
passssionforprogramming Avatar answered Sep 15 '25 07:09

passssionforprogramming