I have implemented bottom bar navigation in my flutter app using go_router and its routes are made as StatefulShellRoute.indexedStack. The current code looks somewhat like this
final _rootNavigatorKey = GlobalKey<NavigatorState>();
final _shellNavigatorAKey = GlobalKey<NavigatorState>(debugLabel: 'shellA');
final _shellNavigatorBKey = GlobalKey<NavigatorState>(debugLabel: 'shellB');
final goRouter = GoRouter(
initialLocation: '/a',
navigatorKey: _rootNavigatorKey,
routes: [
StatefulShellRoute.indexedStack(
builder: (context, state, navigationShell) {
// the UI shell
return ScaffoldWithNestedNavigation(
navigationShell: navigationShell);
},
branches: [
// first branch (A)
StatefulShellBranch(
navigatorKey: _shellNavigatorAKey,
routes: [
// top route inside branch
GoRoute(
path: '/a',
pageBuilder: (context, state) => const NoTransitionPage(
child: RootScreen(label: 'A', detailsPath: '/a/details'),
),
routes: [
// child route
GoRoute(
path: 'details',
builder: (context, state) =>
const DetailsScreen(label: 'A'),
),
],
),
],
),
// second branch (B)
StatefulShellBranch(
navigatorKey: _shellNavigatorBKey,
routes: [
// top route inside branch
GoRoute(
path: '/b',
pageBuilder: (context, state) => const NoTransitionPage(
child: RootScreen(label: 'B', detailsPath: '/b/details'),
),
routes: [
// child route
GoRoute(
path: 'details',
builder: (context, state) =>
const DetailsScreen(label: 'B'),
),
],
),
],
),
],
),
],
);
I implemented a login functionality, the user can login/out on the route '/b'. Since this setup preserves the routes' and branches' states, if, in the first shell branch(A) (index 0), I am at /a/details, and logout while on '/b'. The app would still preserve the state of (A) and stay at /a/details. And it is undesirable if that page's content is user-specific.
So what I was trying to achieve was to 'RESET' the states of all the branches and bring them to initial locations and reload them as well.
The login state is currently maintained in a userAuthProvider that is a state notifier provider.
What have I tried :
I am fairly new to this. With the reference of similiar question's suggested solution here https://stackoverflow.com/questions/77551676/flutter-gorouter-reset-statefullshellroute-branches-to-initial-tabs, I tried achieving it this way.
ref.listen(userAuthProvider, (previous, next) {
final branches = navigationShell.route.branches;
for (final branch in branches) {
try {
branch.navigatorKey.currentState?.popUntil((route) => route.isFirst);
} catch (e) {
continue;
}
}
});
I added this block in the build function of ScaffoldWithNestedNavigation widget to pop the existing pages in the branches. But it doesn't work as expected. When I go back to the first branch with bottom bar again, the pages appear to be popped and then re-added again, so at the end its the same saved screen with old state.