'type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'BannerModel'
every time i get that error and idk why?
class HomeModel {
late bool status;
late DataHomeModel data;
HomeModel.fromjson(Map<String, dynamic> json) {
status = json['status'];
data = DataHomeModel.fromjson(json['data']);
}
}
class DataHomeModel {
List<BannerModel> banners = [];
List<ProductModel> products = [];
DataHomeModel.fromjson(Map<String, dynamic> json) {
json['banners'].forEach((element) {
banners.add(element);
});
json['products'].forEach((element) {
products.add(element);
});
}
}
class BannerModel {
int? id;
String? image;
BannerModel.fromjson(Map<String, dynamic> json) {
id = json['id'];
image = json['image'];
}
}
class ProductModel {
late int id;
late dynamic price;
late dynamic oldprice;
late dynamic discount;
late String name;
late String image;
late bool infavourite;
late bool incart;
ProductModel.fromjson(Map<String, dynamic> json) {
id = json['id'];
price = json['price'];
discount = json['discount'];
oldprice = json['old_price'];
image = json['image'];
name = json['name'];
incart = json['in_cart'];
infavourite = json['in_favourites'];
}
}
Solution 1:[1]
The error most likely comes from the following class while you try to convert the elements to BannerModel elements.
class DataHomeModel {
List<BannerModel> banners = [];
List<ProductModel> products = [];
DataHomeModel.fromjson(Map<String, dynamic> json) {
json['banners'].forEach((element) {
banners.add(element);
});
json['products'].forEach((element) {
products.add(element);
});
}
}
At the line above code, you do not convert the items into the models that you need. So you need to fix that.
class DataHomeModel {
List<BannerModel> banners = [];
List<ProductModel> products = [];
DataHomeModel.fromjson(Map<String, dynamic> json) {
json['banners'].forEach((element) {
banners.add(BannerModel.fromJson(element));
});
json['products'].forEach((element) {
products.add(ProductModel.fromJson(element));
});
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | salihgueler |
