Search code examples
fluttergetterflutter-go-router

The getter 'params' isn't defined for the type "GoRouterState" Flutter


I am trying to resolve the issue with getter, error message:

The getter 'params' isn't defined for the type 'GoRouterState'. Try importing the library that defines 'params', correcting the name to the name of an existing getter, or defining a getter or field named 'params'.

My main.dart code looks following way:

                      GoRoute(
                path: 'session/:level',
                pageBuilder: (context, state) {
                  final levelNumber = int.parse(state.params['level']!);
                  final level = gameLevels
                      .singleWhere((e) => e.number == levelNumber);
                  return buildMyTransition<void>(
                    child: PlaySessionScreen(
                      level,
                      key: const Key('play session'),
                    ),
                    color: context.watch<Palette>().backgroundPlaySession,
                  );
                },
              ),

GoRouterState is defined in another file:

    class ProductCategoryList extends StatelessWidget {
    const ProductCategoryList({super.key});
    @override
    Widget build(BuildContext context) {
    final GoRouterState state = GoRouterState.of(context);
    final Category category = Category.values.firstWhere(
    (Category value) => value.toString().contains(state.params['category']!),
    orElse: () => Category.all,
    );
    final List<Widget> children = ProductsRepository.loadProducts(category: category)
    .map<Widget>((Product p) => RowItem(product: p))
    .toList();
    return Scaffold(
    backgroundColor: Styles.scaffoldBackground,
    body: CustomScrollView(
    slivers: <Widget>[
      SliverAppBar(
        title: Text(getCategoryTitle(category), style: Styles.productListTitle),
        backgroundColor: Styles.scaffoldAppBarBackground,
        pinned: true,
      ),
      SliverList(
        delegate: SliverChildListDelegate(children),
      ),
    ],
  ),
);
}
}

Solution

  • On version 7.0.0 contains some breaking changes including params.

    params and queryParams in BuildContext's namedLocation, pushNamed, pushReplacementNamed replaceNamed have been renamed to pathParameters and queryParameters.

    For your case, it will be

    final levelNumber = int.parse(state.pathParameters['level']!); //better to do a null check