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

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

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

问题

Here is the translated code:

  1. > 2个位置参数应该由'_buildCategoryList'期望,但只找到1个。
  2. 尝试添加缺少的参数
  3. Widget _categoriesList(WidgetRef ref) {
  4. final categories = ref.watch(
  5. categoriesProvider(
  6. PaginationModel(page: 1, pageSize: 10),
  7. ),
  8. );
  9. return categories.when(
  10. data: (list) {
  11. return _buildCategoryList(list!.cast<Category>(), ref);
  12. },
  13. error: (_, __) => const Center(
  14. child: Text("ERR"),
  15. ),
  16. loading: () => const Center(
  17. child: CircularProgressIndicator(),
  18. ));
  19. }
  20. Widget _buildCategoryList(List<Category> categories, WidgetRef ref) {
  21. return Container(
  22. child: ListView.builder(
  23. shrinkWrap: true,
  24. physics: const ClampingScrollPhysics(),
  25. scrollDirection: Axis.horizontal,
  26. itemCount: categories.length,
  27. itemBuilder: (context, index) {
  28. var data = categories[index];
  29. return GestureDetector(
  30. onTap: () {
  31. ProductFilterModel filterModel = ProductFilterModel(
  32. paginationModel: PaginationModel(page: 1, pageSize: 10),
  33. categoryId: data.categoryId);
  34. ref
  35. .read(productsFilterProvider.notifier)
  36. .setProductFilter(filterModel);
  37. ref.read(productsNotifierProvider.notifier).getProducts();
  38. Navigator.of(context).pushNamed("/products", arguments: {
  39. 'categoryId': data.categoryId,
  40. 'categoryName': data.categoryName,
  41. });
  42. },

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

  1. Widget _categoriesList(WidgetRef ref) {
  2. final categories = ref.watch(
  3. categoriesProvider(
  4. PaginationModel(page: 1, pageSize: 10),
  5. ),
  6. );
  7. return categories.when(
  8. data: (list) {
  9. return _buildCategoryList(list!.cast&lt;Category&gt;());
  10. },
  11. error: (_, __) =&gt; const Center(
  12. child: Text(&quot;ERR&quot;),
  13. ),
  14. loading: () =&gt; const Center(
  15. child: CircularProgressIndicator(),
  16. ));
  17. }
  18. Widget _buildCategoryList(List&lt;Category&gt; categories, WidgetRef ref) {
  19. return Container(
  20. child: ListView.builder(
  21. shrinkWrap: true,
  22. physics: const ClampingScrollPhysics(),
  23. scrollDirection: Axis.horizontal,
  24. itemCount: categories.length,
  25. itemBuilder: (context, index) {
  26. var data = categories[index];
  27. return GestureDetector(
  28. onTap: () {
  29. ProductFilterModel filterModel = ProductFilterModel(
  30. paginationModel: PaginationModel(page: 1, pageSize: 10),
  31. categoryId: data.categoryId);
  32. ref
  33. .read(productsFilterProvider.notifier)
  34. .setProductFilter(filterModel);
  35. ref.read(productsNotifierProvider.notifier).getProducts();
  36. Navigator.of(context).pushNamed(&quot;/products&quot;, arguments: {
  37. &#39;categoryId&#39;: data.categoryId,
  38. &#39;categoryName&#39;: data.categoryName,
  39. });
  40. },

答案1

得分: 0

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

  1. Widget _categoriesList(WidgetRef ref) {
  2. final categories = ref.watch(
  3. categoriesProvider(
  4. PaginationModel(page: 1, pageSize: 10),
  5. ),
  6. );
  7. return categories.when(
  8. data: (list) {
  9. return _buildCategoryList(list!.cast<Category>(), ref); // Pass both arguments
  10. },
  11. error: (_, __) => const Center(
  12. child: Text("ERR"),
  13. ),
  14. loading: () => const Center(
  15. child: CircularProgressIndicator(),
  16. ),
  17. );
  18. }
英文:

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

  1. Widget _categoriesList(WidgetRef ref) {
  2. final categories = ref.watch(
  3. categoriesProvider(
  4. PaginationModel(page: 1, pageSize: 10),
  5. ),
  6. );
  7. return categories.when(
  8. data: (list) {
  9. return _buildCategoryList(list!.cast&lt;Category&gt;(), ref); // Pass both arguments
  10. },
  11. error: (_, __) =&gt; const Center(
  12. child: Text(&quot;ERR&quot;),
  13. ),
  14. loading: () =&gt; const Center(
  15. child: CircularProgressIndicator(),
  16. ),
  17. );
  18. }

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:

确定