8

Meaning if I go from a job screen to a client screen (the client the job was for), to another job screen (another job done for the client) etc, how can I display job > client > job?

And including parameters, so I could display Job 12 > SomeCompany > Job 17.

Sub routes aren't sufficient because the stack can repeat through multiple of the same pages infinitely.

Hrishikesh Kadam
  • 35,376
  • 3
  • 26
  • 36
Tristan King
  • 438
  • 4
  • 17

3 Answers3

6

I have found one solution which relies on the internal API implementation.
This might break in the future so use it with care.

GoRouter.of(context).routerDelegate.currentConfiguration.matches

Returns List<RouteMatch>

ⓘ Tip for Beginners

Make sure you have configured the routes correctly in GoRouter().
Hope they are nested as the route goes down the navigation instead of being siblings to each other.

Also, make sure that you give the whole URI location string to GoRouter.of(context).go() like '/home/level-2/level-3'

Hrishikesh Kadam
  • 35,376
  • 3
  • 26
  • 36
2

This isn't possible with go_router. auto_route has an API to check the stack, but go_router shows no search results for stack.


Instead of GoRouter.of like in this answer, you could use the extension method BuildContext#canPop. For example, in my onboarding page, I have this logic to pop if I can, and if not possible (the first time someone launches the app), I replace the page.:

if (context.canPop()) {
  context.pop();
} else {
  context.replace(Routes.dashboard);
  // Or alternatively, allow the user to navigate back to onboarding with:
  //  context.push(Routes.dashboard);
}
Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
1

I'm not sure about checking the whole stack , but in case of anybody needed to check if there is a page on the stack , GoRouter has a canPop() method:

/// Returns `true` if there is more than 1 page on the stack. bool canPop() => GoRouter.of(this).canPop();

ParSa
  • 1,118
  • 1
  • 13
  • 17