Search code examples
flutterflutter-dependencies

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


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

Solution

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