Home > Mobile >  Map<String, dynamic>' is not a subtype of type 'CategoryModel')
Map<String, dynamic>' is not a subtype of type 'CategoryModel')

Time:01-14

i'm trying to fetch data using GetX in flutter. Whenever I try to show the data I always get the following error:

Map<String, dynamic>' is not a subtype of type 'CategoryModel').

This is my GetBuilder:

GetBuilder<CategoriesController>(builder: (categories) {
                    return GridView.count(
                      crossAxisCount: 4,
                      shrinkWrap: true,
                      physics: ScrollPhysics(),
                      mainAxisSpacing: 8,
                      crossAxisSpacing: 8,
                      childAspectRatio: 60 / 100,
                      children: List.generate(categories.categoriesList.length,
                          (index) {
                        return _buildSingleCategory(
                            index, categories.categoriesList[index]!);
                      }),
                    );
                  }),

And this is my _buildSingleCategory widget:

Widget _buildSingleCategory(int position, CategoryModel category) {
    return InkWell(
        onTap: () {
          Navigator.push(
              context,
              new MaterialPageRoute(
                  builder: (context) => new ProductListPage()));
        },
        child: Container(
          child: Column(
            children: [
              Container(
                padding: EdgeInsets.all(16),
                margin: EdgeInsets.only(bottom: 5),
                width: double.infinity,
                height: 80,
                decoration: style.shadowContainer(),
                child: Image.asset('assets/images/kale.png'),
              ),
              Text(
                category.name,
                style: TextStyle(fontSize: 13),
              ),
            ],
          ),
        ));
  }

And this is my model:

class CategoryModel {
  final int id;
  final String name;
  final String photo;

  CategoryModel({required this.id, required this.name, required this.photo});

  factory CategoryModel.fromJson(Map<String, dynamic> json) => CategoryModel(
        id: json["id"],
        name: json["name"],
        photo: json["photo"],
      );
}

List<CategoryModel> categoryModelFromJson(String str) {
  return List<CategoryModel>.from(json.decode(str).map((x) => CategoryModel.fromJson(x)));
}

I'd be glad if someone helps me with this, thanks in advance!

CodePudding user response:

I think categories.categoriesList is a list of Map<String, dynamic>. Make sure it is a List of CategoryModel.

CodePudding user response:

I see that you're declaring _buildSingleCategory to accept an int and CategoryModel :

Widget _buildSingleCategory(int position, CategoryModel category) {
// ...

but here you're passing the wrong types :

// ...
(index) {
  return _buildSingleCategory(
        index, categories.categoriesList[index]!);
 }),

I assume here that the categories.categoriesList[index]! is a Map<String, dynamic>, so what you need it to modelize it to a CategoryModel with the fromJson method like this:

GetBuilder<CategoriesController>(builder: (categories) {
                    return GridView.count(
                      crossAxisCount: 4,
                      shrinkWrap: true,
                      physics: ScrollPhysics(),
                      mainAxisSpacing: 8,
                      crossAxisSpacing: 8,
                      childAspectRatio: 60 / 100,
                      children: List.generate(categories.categoriesList.length,
                          (index) {
                        return _buildSingleCategory(
                            index, CategoryModel.fromJson(categories.categoriesList[index]!)); // check this
                      }),
                    );
                  }),
  • Related