dieppa
dieppa

Reputation: 190

Why Flutter GoRouter ignores pageKey

Regarding the documentation if Flutter identifies the same key it will update the url and keep the page (Widget). In the code below pageKey is a static const value set in both Containers. When navigating between those two routes (/home & /home/login) Flutter ignores it's the same widget and recreates it.

class ProjectXApp extends StatelessWidget {

  ProjectXApp({Key? key}) : super(key: key);

  static const pageKey = ValueKey("Home");

  final _router = GoRouter(
    routes: [
      GoRoute(
        path: '/',
        redirect: (_) => Routes.routeHome,
      ),
      GoRoute(
        path: '/home',
        builder: (context, state) {
          return Container(key: pageKey);
        }
      ),
      GoRoute(
          path: "/home/login",
          builder: (context, state) {
            return Container(key: pageKey);
          }
      )
    ],
  );

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
        routeInformationParser: _router.routeInformationParser,
        routerDelegate: _router.routerDelegate
    );
  }
}

Why is it happening? Is there a way to achieve this?

Upvotes: 1

Views: 1127

Answers (1)

nolan
nolan

Reputation: 1

You are understanding the documentation correctly. I ran into this same issue and I found that switching to context.go(...) from context.push(....) gave me the desired result. Hope this helps.

Upvotes: 0

Related Questions