class Brand { final String id; final String brandName; List images; Brand({ required this.id, required this.brandName, required this.images, }); factory Brand.fromJson(Map json) { return Brand( id: json['_id'], brandName: json['brandName'], // images: json["image"] != null // ? (json["image"] as List).map((e) => BrandImage.fromJson(e)).toList() // : [], images: json["image"] != null // Ensure you use the correct key ? (json["image"] as List) .map((e) => BrandImage.fromJson(e)) .toList() : [], ); } Map toJson() { return { '_id': id, 'brandName': brandName, "image": images.map((e) => e.toJson()).toList(), }; } @override String toString() { return 'Brand(id: $id, brandName: $brandName,images: $images)'; } } class BrandImage { String publicId; String url; String imageId; BrandImage({ required this.publicId, required this.url, required this.imageId, }); factory BrandImage.fromJson(Map json) { return BrandImage( publicId: json["public_id"] ?? "", url: json["url"]?.toString() ?? "", imageId: json["_id"] ?? "", ); } Map toJson() { return { "public_id": publicId, "url": url, "_id": imageId, }; } @override String toString() { return "BrandImage(publicId: $publicId, url: $url, imageId: $imageId)"; } }