How can I fix the error '2 positional arguments expected by '_buildCategoryList', but 1 found.' in flutter dependencies?

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

How can I fix the error '2 positional arguments expected by '_buildCategoryList', but 1 found.' in flutter dependencies?

问题

Here is the translated code:

> 2个位置参数应该由'_buildCategoryList'期望,但只找到1个。

尝试添加缺少的参数

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

Widget _buildCategoryList(List<Category> categories, WidgetRef ref) {
 return Container(
      child: ListView.builder(
        shrinkWrap: true,
        physics: const ClampingScrollPhysics(),
        scrollDirection: Axis.horizontal,
        itemCount: categories.length,
        itemBuilder: (context, index) {
          var data = categories[index];
          return GestureDetector(
            onTap: () {
              ProductFilterModel filterModel = ProductFilterModel(
                  paginationModel: PaginationModel(page: 1, pageSize: 10),
                  categoryId: data.categoryId);

              ref
                  .read(productsFilterProvider.notifier)
                  .setProductFilter(filterModel);
              ref.read(productsNotifierProvider.notifier).getProducts();
              Navigator.of(context).pushNamed("/products", arguments: {
                'categoryId': data.categoryId,
                'categoryName': data.categoryName,
              });
            },

Please note that I added the missing argument ref to the _buildCategoryList function call as required.

英文:

> 2 positional arguments expected by '_buildCategoryList', but 1 found.

Try adding the missing arguments

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

Widget _buildCategoryList(List&lt;Category&gt; categories, WidgetRef ref) {
 return Container(
      child: ListView.builder(
        shrinkWrap: true,
        physics: const ClampingScrollPhysics(),
        scrollDirection: Axis.horizontal,
        itemCount: categories.length,
        itemBuilder: (context, index) {
          var data = categories[index];
          return GestureDetector(
            onTap: () {
              ProductFilterModel filterModel = ProductFilterModel(
                  paginationModel: PaginationModel(page: 1, pageSize: 10),
                  categoryId: data.categoryId);

              ref
                  .read(productsFilterProvider.notifier)
                  .setProductFilter(filterModel);
              ref.read(productsNotifierProvider.notifier).getProducts();
              Navigator.of(context).pushNamed(&quot;/products&quot;, arguments: {
                &#39;categoryId&#39;: data.categoryId,
                &#39;categoryName&#39;: data.categoryName,
              });
            },

答案1

得分: 0

Since your _buildCategoryList widget has two arguments but you are
passing only one argument.

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); // Pass both arguments
    },
    error: (_, __) => const Center(
      child: Text("ERR"),
    ),
    loading: () => const Center(
      child: CircularProgressIndicator(),
    ),
  );
}
英文:

Since your _buildCategoryList widget has two arguments but you are
passing only one argument.

Widget _categoriesList(WidgetRef ref) {
  final categories = ref.watch(
    categoriesProvider(
      PaginationModel(page: 1, pageSize: 10),
    ),
  );
  return categories.when(
    data: (list) {
      return _buildCategoryList(list!.cast&lt;Category&gt;(), ref); // Pass both arguments
    },
    error: (_, __) =&gt; const Center(
      child: Text(&quot;ERR&quot;),
    ),
    loading: () =&gt; const Center(
      child: CircularProgressIndicator(),
    ),
  );
}

huangapple
  • 本文由 发表于 2023年5月21日 09:30:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297946.html
匿名

发表评论

匿名网友

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

确定