I’m facing an issue with Go Router in Flutter where the current page gets reloaded when pushing to a new page. Specifically, when I’m on the “/home” page (which is a part of a shell route) and push to the “/staff_profile” page using context.push, the “/home” page gets reloaded in the background.

Here’s the relevant code snippet from my Go Router configuration:

Home

static final router = GoRouter(
    navigatorKey: _mainNavigatorKey,
    observers: [
      GoRouterObserver(
        analytics: FirebaseAnalytics.instance,
      )
    ],
    routes: [
      ShellRoute(
        observers: [
          GoRouterObserver(
            analytics: FirebaseAnalytics.instance,
          )
        ],
        builder: (context, state, child) {
          ... // code
        },
        routes: <RouteBase>[
          GoRoute(
            path: '/home',
            builder: (context, state) => HomeFragemnt(
              onNotificationIconTab: (){

              }, userRepository: RepositoryProvider.of<UserRepository>(context),
              coSoRepository: RepositoryProvider.of<CoSoRepository>(context),
              reportRepository: RepositoryProvider.of<ReportRepository>(context),
              onTabDetailTipSummary: (branchGuid , timeReport , time ) {
                context.push('/technician_tip_league?branchGuid=$branchGuid&timeReport=$timeReport&time=$time');
              },
              onTabStaff: (staffGuid){
                context.push('/staff_profile?nhanVienGuid=$staffGuid');
              }, onRevenueTimeChartTab: (branchGuid , timeReport ) {
              context.push('/revenue_by_parent_time?branchGuid=$branchGuid&timeReport=$timeReport');
            },
            ),
          ),

staff_profile

 GoRoute(
        path: '/resource_staff',
        builder: (context, state){
          var branchGuid = state.uri.queryParameters['branchGuid'].toString();
          return StaffSummaryDetail(
            reportRepository: RepositoryProvider.of<ReportRepository>(context),
            branchGuid: branchGuid,
            onTabStaffInfo: (postion , ageMin , ageMax , coSoGuid ) {
              context.push('/staff_list?position=$postion&ageMin=$ageMin&ageMax=$ageMax&coSoGuid=$coSoGuid');
            },
          );
        },
      ),

The expected behavior is that the “/home” page should not reload when pushing the “/staff_profile” page onto the navigation stack. However, if I use Navigator.push instead of context.push, the current page does not reload in the background.

Can anyone help me understand why the current page is reloading when using context.push with Go Router, and suggest a solution to prevent this behavior, especially when the current page is a part of a shell route? I’ve tried both pageBuilder and builder for the home route, but the issue persists.

Please let me know if you need any additional information or clarification.

New contributor

user24003634 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.