Search code examples
flutterdartflutter-go-router

Parameter passing causes an error when using go_router


I can't access state.queryParameters during routing with Go router. In the doc the following example is given:

GoRoute(
  path: '/users',
  builder: (context, state) => const UsersScreen(filter: state.queryParameters['filter']),
)

Here's how I implemented the whole thing:

final GoRouter _router = GoRouter(    
    routes: [
      GoRoute(
          path: "/",
          builder: (context, state) => const Dashboard(),
          routes: [
       
        // 1. pathParameter
        GoRoute(path: "profile/:name", builder: (context, state) => Profile(
          name: state.pathParameters["name"]!
        )),
        // 2. queryParameter
        GoRoute(path: "settings", builder: (context, state) => Settings(
            name: state.queryParameters['name']!
        )),
      ]),
    ]
  );

While it works fine with pathParameter, state.queryParameters cannot be accessed. I get following error message: Error: The getter 'queryParameters' isn't defined for the class 'GoRouterState'.

I use go_router: 10.1.2. Does anyone know what to do here?


Solution

  • There is a breaking change with the latest go_router packages which replaces accessing the location, queryParamters, and queryParameterAll with uri

    so instead of accessing the queryParamters like this

    GoRoute(path: "profile/:name", builder: (context, state) => Profile(
      name: state.pathParameters["name"]!
    )),
    state.queryParameters['name']!
    

    it has been replaced with this

    GoRoute(path: "profile/:name", builder: (context, state) => Profile(
      name: state.uri.queryParameters["name"]!
    )),
    

    you can read more about it here go_router_migration_guide