In my project I am using go_router: ^12.1.3
. I am having trouble with the back button while using router.pop()
/context.pop()
.
My route is something like /community_tab_selected/0/profile_subroute_from_community
. This redirects from the Community Screen to the Profile Screen in my app. When I press the back button in Profile Screen using router.pop()
/context.pop()
, I want it to redirect to /community_tab_selected/0
but it redirects me to /dashboard
which is base route. How can I achieve proper pop() functionality so that it pops the shell route(profile_subroute_from_community
) and leads me to the route of /community_tab_selected/0
Community route
final communityTab = GoRoute(
name: ROUTE.community_tab_selected,
path: '/community_tab_selected/:index',
pageBuilder: (context, state) {
return createRoutePage(
state.pageKey,
DashboardPage(
initialIndex: "1",
communityTabParams:
CommunityChannelPageQueryParams.fromJson(state.pathParameters),
),
opaque: false,
);
},
routes: [
ShellRoute(
builder: (context, state, child) {
return child;
},
routes: [
profileSubrouteFromCommunity,
],
),
],
);
ShellRoute for Profile
final profileSubrouteFromCommunity = GoRoute(
name: ROUTE.profile_subroute_from_community,
path: 'profile_subroute_from_community/:userid',
pageBuilder: (context, state) {
return createRoutePage(
state.pageKey,
const ProfilePage(),
opaque: false,
);
},
);
2
Answers
If you want to keep you navigation history using GoRouter…
Make sure that you are using Router.push() or context.push instead of the Router.go() or context.go()
Always use the push() if you want to keep the Navigation history.
Unfortunately I ran into the same issues with
StatefulShellRoute.indexedStack
but the logic should be the same forShellRoute
‘s as well.It’s also unfortunate that I needed to rely on the native navigator than on GoRouter in this case.
The following piece of code pops all of the
ShellRoute
s from the navigation stack:The following code pops all of the Routes until the first route is encountered: (
rootNavigator: true
might be optional).We will have to rely on the native old Navigator for this until it gets support from the Flutter team
You can reach the whole code in this gist.
Let me know if this makes sense for you. Hope this answer and gist becomes a reference to others as well.