Skip to content

[go_router] GoRouterState regression #111040

@rzetzsche

Description

@rzetzsche

The behavior of the GoRouter state inside the builder function changed from go_router 3 to 4. Before the change I always received the full qualified location inside GoRouterState which was opened by the user, regardless of the sub route. With go_router 4 I only get the current sub route location.

So If I have the following routes:

[GoRouter] known full paths for routes:
[GoRouter]   => /
[GoRouter]   =>   /family
[GoRouter]   =>     /family/:pid
[GoRouter]   =>       /family/:pid/info

And I opt to open the following location /family/123/info with go_router 4 I will get the following builder calls:

/ => state.location = "/"
/family => state.location = "/family"
/family/:pid=> state.location = "/family/123"
/family/:pid/info => state.location = "/family/123/info"

but before the change I got:

/ => state.location = "/family/123/info"
/family => state.location = "/family/123/info"
/family/:pid=> state.location = "/family/123/info"
/family/:pid/info => state.location = "/family/123/info"

The change happened inside flutter/packages#2189

I asked for the motivation of the change inside the merge request here. In response @johnpryan asked me to file a new Issue.

Steps to Reproduce

  1. Define sub routes and print the actual location of the GoRouterState or use example code
  2. Compare outputs for go_router 3 and go_router 4

Expected results:

GoRouterState should contain the full path which was opened by the user in every sub route's builder call.

The code provided should print:

root: /family/123/info
family: /family/123/info
familyChild: /family/123/info
familyChildInfo: /family/123/info

Actual results:

GoRouterState only contains relative path of the sub route.

The code provided prints:

root: /
family: /family
familyChild: /family/123
familyChildInfo: /family/123/info
Code sample
class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    final router = GoRouter(
      routes: <GoRoute>[
        GoRoute(
          path: "/",
          builder: (context, state) {
            print("root: ${state.location}");
            return Container();
          },
          routes: [
            GoRoute(
              path: "family",
              builder: (context, state) {
                print("family: ${state.location}");
                return Container();
              },
              routes: [
                GoRoute(
                  path: ":pid",
                  builder: (context, state) {
                    print("familyChild: ${state.location}");
                    return Container();
                  },
                  routes: [
                    GoRoute(
                      path: "info",
                      builder: (context, state) {
                        print("familyChildInfo: ${state.location}");
                        return Container();
                      },
                    ),
                  ],
                ),
              ],
            )
          ],
        )
      ],
      initialLocation: "/family/123/info",
    );

    return MaterialApp.router(
      routeInformationProvider: router.routeInformationProvider,
      routeInformationParser: router.routeInformationParser,
      routerDelegate: router.routerDelegate,
    );
  }
}
Logs

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listp: go_routerThe go_router packagepackageflutter/packages repository. See also p: labels.

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions