You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter报错:类型'GoRouterState'未定义getter 'params'

解决GoRouterState中params未定义的报错

报错原因是GoRouter版本更新后,GoRouterState不再提供params属性,替代的是pathParameters(获取路径中的动态参数)和queryParameters(获取URL查询参数)。你的代码里用的是路径参数,直接替换成pathParameters即可。

修改后的代码示例

1. main.dart中的GoRoute部分

GoRoute(
  path: 'session/:level',
  pageBuilder: (context, state) {
    // 替换state.params为state.pathParameters
    final levelNumber = int.parse(state.pathParameters['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,
    );
  },
),

2. ProductCategoryList组件部分

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(
      // 替换state.params为state.pathParameters
      (Category value) => value.toString().contains(state.pathParameters['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),
          ),
        ],
      ),
    );
  }
}

如果你的项目依赖的是较旧版本的GoRouter(比如3.x及以下),params属性是存在的,但建议升级到最新稳定版并使用上述新API,旧API已被废弃。

内容的提问来源于stack exchange,提问作者Elena Epshtein

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 10:52:13