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

Flutter中如何修复‘_buildCategoryList需2个位置参数,仅传入1个’的错误?

解决Flutter中_buildCategoryList参数不匹配的错误

错误提示2 positional arguments expected by '_buildCategoryList', but 1 found的原因很明确:你定义的_buildCategoryList方法需要两个位置参数(List<Category>和WidgetRef),但调用时只传入了第一个参数。

修改方法

在调用_buildCategoryList的时候,把当前方法的ref参数传进去即可:

修改_categoriesList方法中的调用代码:

data: (list) {
  // 补充传入ref参数
  return _buildCategoryList(list!.cast<Category>(), ref);
},

修改后的完整_categoriesList方法代码:

Widget _categoriesList(WidgetRef ref) {
  final categories = ref.watch(
    categoriesProvider(
      PaginationModel(page: 1, pageSize: 10),
    ),
  );
  return categories.when(
      data: (list) {
        return _buildCategoryList(list!.cast<Category>(), ref);
      },
      error: (_, __) => const Center(
            child: Text("ERR"),
          ),
      loading: () => const Center(
            child: CircularProgressIndicator(),
          ));
}

这样就能满足_buildCategoryList方法的参数要求,解决参数不匹配的问题——因为_buildCategoryList内部需要用ref来读写状态管理的Provider,所以必须传入这个参数。

内容的提问来源于stack exchange,提问作者Chiến Trần

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 05:07:04