13

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?

Peter Irving
  • 305
  • 2
  • 10
  • For clarification: Why is `context.push()` not fitting in your case? With .go you will replace the whole navigation stack so you can't "add" another route with it – jraufeisen Aug 23 '22 at 12:25
  • Honestly I probably did a whole unnecessary workaround. Please see my Edit above to explain the entire issue I was trying to solve – Peter Irving Aug 23 '22 at 19:36
  • Your edit was very helpful, thanks. But I am really suprised about your obversations. When `MyPage.build()` is called, then this will lead to a visual update. Are you sure that ths is the problem? Do you want to show the code where you call `context.go` and the contents of `MyPage.build`? – jraufeisen Aug 24 '22 at 14:13
  • 3
    @PeterIrving: were you able to find a solution please ? I’m facing the same issue and both the answers below doesn’t seem to work. Any help would be much appreciated. – midNight Nov 04 '22 at 02:05
  • Did someone find a solution about this problem ? @PeterIrving – Madere Jul 11 '23 at 07:14

3 Answers3

11

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),
        );
    },
),
Pablo Insua
  • 852
  • 11
  • 19
0

So this problem will only show up if the route is nested (shell routes) and you have pushed a new route using context.pushNamed('route-name') or context.push('name').

You can simply await the child route to pop using the following code snippet.

onTap: () async {
  final bool? result = await context.push<bool>('/page2');
  if(result ?? false)...
  // TODO: here can be the refresh logic
}

This is mentioned in go_router docs. This won't reinitiate the page. . If you want to reinitiate the page every time the page shows up. The answer will be using UniqueKey() as suggested above. But this will cause issue with transition animation.

Ritesh Singh
  • 782
  • 9
  • 19
-1

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