'_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'BanarModel'

Viewed 39

what is the wrong here .I think because I stored object in the list so how I can solve it the Wrong is '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'BanarModel'.

class HomeModel 
{
  bool status;
  HomeDataModel data;  
  HomeModel.fromJson(Map<String, dynamic> json) {
  status = json['status'];
  data = HomeDataModel.fromJson(json['data']);
 }
}

class HomeDataModel 
{
  List<BanarModel> banners; 
  HomeDataModel.fromJson(Map<String, dynamic> json) {
  json['banners'].forEach((element) {
  banners.add(element);
  });   
 }
}   

class BanarModel 
{
  var id;
        String token;
        String image;
        BanarModel.fromJson(Map<String, dynamic> json) {
        id = json['id'];
        token = json['token'];
        image = json['image'];
        }
}   
HomeModel homeModel;
  void getHomeData(){
    emit(ShopLoadingHomeDataStat());
   DioHelper.getData(url: Home,token:token ,).then((value){
   homeModel=HomeModel.fromJson(value.data);
  print(homeModel.data.banners[0].image);
   emit(ShopSuccessHomeDataStat());

 }).catchError((onError){
   print(onError.toString());
   emit(ShopErorrHomeDataStat(error: onError));
 });

i call it directly when openning the app

BlocProvider(create: (Context)=>ShopCubit()..getHomeData()) 
1 Answers

Update your HomeDataModel class like this below

HomeDataModel.fromJson(Map<String, dynamic> json) {
   banners = (json['banners'] as List)?.map((e) => e == null ? null : BanarModel.fromJson(e as Map<String, dynamic>))?.toList();
}
Related