The getter ‘params’ isn’t defined for the type “GoRouterState” Flutter.

huangapple go评论84阅读模式
英文:

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 the following way:

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),
          ),
        ],
      ),
    );
  }
}

Is there anything else you would like me to translate or assist with?

英文:

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),
      ),
    ],
  ),
);
}
}

答案1

得分: 4

在版本7.0.0中包含了一些重大变更,包括params

paramsqueryParams在BuildContext的namedLocation、pushNamed、pushReplacementNamed和replaceNamed中已被重命名为pathParametersqueryParameters

对于你的情况,应该是

final levelNumber = int.parse(state.pathParameters['level']!); //最好进行空值检查
英文:

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

huangapple
  • 本文由 发表于 2023年6月12日 04:37:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定