英文:
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
。
params
和queryParams
在BuildContext的namedLocation、pushNamed、pushReplacementNamed和replaceNamed中已被重命名为pathParameters
和queryParameters
。
对于你的情况,应该是
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论