skip to Main Content

I use go_router’s StatefulShellRoute.indexedStack to implement a design with bottomNavBar. How can I reset the current branch to its original state AND go to another branch at the same time?

Tab A is used to create or edit a single item. In A, I have some routes for a multi-step process to gather data for a new item. Eventually, the item is created and the user is sent to another tab C using context.goNamed(AppRoute.tabC.name)

The problem is that tab A’s state is retained during the redirect. So if the user later switches back to that tab, they will see the last form of the data gathering process, not the first form ready to accept fresh inputs.

Question: How can I make sure tab A is reset to the initial route while jumping to a different tab?

Update: I’ve used a fork of the official stateful_shell_route example in the go_router package to demonstrate what I want to accomplish.
https://github.com/githubmonkey/flutter_packages/commit/bbeec93c14e33d1294bc06584f07662f1909cc80

As you can see, my naive approach of using consecutive MyRouter.go() statements is not sufficient.

2

Answers


  1. Something like…

    import 'package:go_router/go_router.dart';
    
    final GlobalKey<TabAState> tabAKey = GlobalKey<TabAState>();
    
    void navigateToTabB(BuildContext context) {
      goRouter.resetTo(
        AppRoute.tabAStart.name, 
        navigatorKey: goRouter.navigatorKey,
        builder: (BuildContext context, StatefulRouter router, int tabIndex) {
          switch (tabIndex) {
            case 0:
              return TabA(key: tabAKey);
            case 1:
              return TabB();
            default:
              return Container();
          }
        },
      );
    
    
      context.goNamed(AppRoute.orders.name);
    }
    
    
    Login or Signup to reply.
  2. Mostly a bug.

    A workaround is to have a small delay before the second .go:

    GoRouter.of(context). go('/a');
    Future.delayed(
      const Duration(milliseconds: 100),
      () => GoRouter.of(context).go('/c'),
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search