Home > Mobile >  Flutter - Dart parsing json data array returns type 'List<dynamic>' is not a subtype
Flutter - Dart parsing json data array returns type 'List<dynamic>' is not a subtype

Time:07-20

I'm trying to parse a json file using a custom model, I always get the error type 'List<dynamic>' is not a subtype of type 'List<BusinessTest>' and I don't know how I can fix my code. Also is it a good idea to always use nullable type in variables when you parse json files?

This is a Json example of my data:

{
    "businesses": [{
            "id": "1",
            "alias": "123",
            "name": "aaa",
            "image_url": "xxx.jpg",
            "is_closed": false,
            "url": ".com",
            "review_count": 26,
            "rating": 5.0
        },
        {
            "id": "2",
            "alias": "123",
            "name": "aaa",
            "image_url": "xxx.jpg",
            "is_closed": false,
            "url": ".com",
            "review_count": 26,
            "rating": 5.0
        }
    ]
}

Here is the model code I've made in order to parse the Json:

class BusinessSearch {
  final List<BusinessTest> businesses;
  final int total;

  BusinessSearch(this.businesses, this.total);

  BusinessSearch.fromJson(Map<String, dynamic> json)
      : businesses = json['businesses'],
        total = json['total'];
}

class BusinessTest {
  final String? name;
  final String? imageUrl;
  final bool? isClosed;
  final String? url;
  final int? reviewCount;

  BusinessTest(
      this.name, this.imageUrl, this.isClosed, this.url, this.reviewCount);

  BusinessTest.fromJson(Map<String, dynamic> json)
      : name = json['name'],
        imageUrl = json['image_url'],
        isClosed = json['is_closed'],
        url = json['url'],
        reviewCount = json['review_count'];
}
 

This is how I'm trying to parse it:

void getData() async {
    try {
      String url =  'url';

      NetworkHelp network = NetworkHelp(url: url);
      var data = await network.getData();

      Map<String, dynamic> businessMap = await jsonDecode(data);
      var business = BusinessSearch.fromJson(businessMap);

    } catch (e) {
      print(e);
    }
  }

CodePudding user response:

You have to update your BusinessSearch model like this.

class BusinessSearch {
    BusinessSearch({
        this.businesses,
        this.total,
    });

    List<Business> businesses = [];
    int total;

    factory BusinessSearch.fromJson(Map<String, dynamic> json) => BusinessSearch(
        businesses: List<Business>.from(json["businesses"].map((x) => Business.fromJson(x))),
        total: json['total']
    );

    Map<String, dynamic> toJson() => {
        "businesses": List<dynamic>.from(businesses.map((x) => x.toJson())),
        "total": total,
    };
}

class Business {
    Business({
        this.id,
        this.alias,
        this.name,
        this.imageUrl,
        this.isClosed,
        this.url,
        this.reviewCount,
        this.rating,
    });

    String id;
    String alias;
    String name;
    String imageUrl;
    bool isClosed;
    String url;
    int reviewCount;
    int rating;

    factory Business.fromJson(Map<String, dynamic> json) => Business(
        id: json["id"],
        alias: json["alias"],
        name: json["name"],
        imageUrl: json["image_url"],
        isClosed: json["is_closed"],
        url: json["url"],
        reviewCount: json["review_count"],
        rating: json["rating"],
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "alias": alias,
        "name": name,
        "image_url": imageUrl,
        "is_closed": isClosed,
        "url": url,
        "review_count": reviewCount,
        "rating": rating,
    };
}
  • Related