go_router causing all screens in the Navigation stack to rebuild on each push-and pop operation
Asked Answered
N

0

7

I'm using go_router 10.0.0 and it rebuilds every screen in the navigation stack every time I do push or pop. All screens are StatlessWidget and declare it as const constructors as I'm saving the state of each screen using State Management (using BLOC)

This behavior causes unnecessary screen build, unnecessary API calls and makes the screen lose its state How to solve This? My code

  static GoRouter appRoutes = GoRouter(
    navigatorKey: _navKey,
    initialLocation: "/${_intialRout(_hiveManager, _configRoute)}",
    observers: AppConstant.navigatorObservers(),
    routerNeglect: true,
    routes: [
    
      ///SignInMethodsScreen
      GoRoute(
        name: Routes.signInMethods,
        path: "/${Routes.signInMethods}",
        builder: (context, state) => const SignInMethodsScreen(),
      ),

      ///EnterEmailScreen
      GoRoute(
        name: Routes.enterEmailScreen,
        path: "/${Routes.enterEmailScreen}",
        builder: (context, state) => const EnterEmailScreen(),
      ),

      ///SignInScreen
      GoRoute(
        name: Routes.signinScreen,
        path: "/${Routes.signinScreen}",
        builder: (context, state) => const SignInScreen(),
      ),

      ///OtpScreen
      GoRoute(
        name: Routes.otpScreen,
        path: "/${Routes.otpScreen}",
        builder: (context, state) => const OtpScreen(),
      ),

      ///CreatePasswordScreen
      GoRoute(
        name: Routes.createPasswordScreen,
        path: "/${Routes.createPasswordScreen}",
        builder: (context, state) => const CreatePasswordScreen(),
      ),

      ///MainScreen
      GoRoute(
        name: Routes.mainScreen,
        path: "/${Routes.mainScreen}",
        builder: (context, state) => const MainScreen(),
      ),

 
    ],
  );
Noelyn answered 16/8, 2023 at 9:6 Comment(3)
Why are you using navKey?Bartko
@Bartko it's because of another functionality, btw the cause of the rebuild is using GeRouterState.of(context) inside the build methodNoelyn
I switched to auto route => pub.dev/packages/auto_routeNoelyn

© 2022 - 2024 — McMap. All rights reserved.