I’ve tried and tried, but go_router and flutter won’t do the obvious.
I just want the back button to go back to the immediately previous route that called .go on the current route.
But to no avail:
GoRouter.of(context).pop()
does whatever it wants. Sometimes it goes back to the immediately previous route, other times it goes back to ‘/’.
I’m using Typed Routes and of course that means that you have to have a / route that goes to basically nothing because it will get called ALWAYS, instead of just when that page is actually routed to.
Here’s what I have for routes:
@TypedGoRoute<HomePageRoute>(path: '/', routes: [
TypedGoRoute<ErrorPageRoute>(path: 'errors/error'),
TypedGoRoute<LocationServicesErrorRoute>(path: 'errors/location'),
TypedGoRoute<LoginPageRoute>(path: 'authentication/login'),
TypedGoRoute<PasswordPageRoute>(path: 'authentication/password'),
TypedGoRoute<ForgotPasswordPageRoute>(path: 'authentication/forgotpassword'),
TypedGoRoute<RegistrationRoute>(path: 'registration'),
TypedGoRoute<SettingsPageRoute>(path: 'settings'),
TypedGoRoute<RideHomePageRoute>(path: 'ride'),
TypedGoRoute<RideNowPageRoute>(path: 'ride/now'),
TypedGoRoute<RideNowSelectDriverPageRoute>(path: 'ride/now/selectdriver'),
TypedGoRoute<RideLaterPageRoute>(path: 'ride/later'),
TypedGoRoute<DriverVerificationStatusPageRoute>(path: 'verification/drivers'),
TypedGoRoute<StartDrivingPageRoute>(
path: 'verification/drivers/startdriving'),
TypedGoRoute<GetVerifiedPageRoute>(path: 'verification/riders'),
])
class HomePageRoute extends GoRouteData {
@override
Widget build(BuildContext context, GoRouterState state) {
return DefaultHomePage(
key: state.pageKey,
);
}
}
Here’s my router:
final router = GoRouter(
navigatorKey: navigationKey,
routes: $appRoutes,
}
I use .go(context) on the routes to navigate to the next screen.
If I actually create a hierarchy in those routes (i.e. the authentication section) with the routes:[] list in stuff, then it NEVER just goes back to the previous route, it ALWAYS goes back to the parent.
How do I stop this madness and get go_router to just do what I think is self evident: when you call pop() it goes back to the immediate previous page that called .go(context) to get there?
(Yes I get that dialogs etc. use this too and subvert this, but I’m not talking about dialogs)
Bonus if I can get it to completely collapse the stack somehow and have ONLY the new typed route in the stack too. (i.e. when I call .go(context) on my home route, I want the entire stack to go away and the only thing in it be the homeroute.