skip to Main Content

i am doing GET api then specialized to model and i am getting this error, i am not sure what to do

json Link : https://api-zingmp3-vercel.vercel.app/api/home

enter image description here

2

Answers


  1. You should cast the json item list before calling forEach():

    (json['items'] as List).forEach()
    
    Login or Signup to reply.
  2. if (json['items'] != null) {
    
      json['items'].forEach((e) {
        items.add(Item.fromJson(e));
    });
    ...
    }
    

    Try changing the code above to A, B and C.

    A

      if (json['items'] != null) {
    
         var itemBox = json[“items"] as List<Map<String, dynamic>>;
         itemBox.forEach((e) {
            items.add(Item.fromJson(e));
         });
         ...
      }
    

    B

      if (json['items'] != null) {
    
         var itemBox = json[“items"] as List<Map<String, dynamic>>;
         itemBox.map<Item>((e) => testList.add(Item.fromJson(e)) as Item);
         ...
      }
    

    C

     if (json['items'] != null) {
    
        items = List<Item>.from(
        (json['items'] as List<Map<String, dynamic>>).map((x) => Item.fromJson(x)));
        ...
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search