I cannot figure out the way to adapt my dart model to my json. I constantly get the error in BreedInfo class, field height:
_TypeError (type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String?')
I do not know why it does not accept a response if dynamic is supposed to identify the data type automatically and therefore would be a String anyway.
My dart model:
import 'dart:core';
class DogClass {
Id? _iId;
String? _breed;
String? _origin;
String? _url;
String? _img;
BreedInfo? _breedInfo;
BreedInfo? _breedInfo1;
List<String>? _behavior;
DogClass(
{Id? iId,
String? breed,
String? origin,
String? url,
String? img,
BreedInfo? breedInfo,
BreedInfo? breedInfo1,
List<String>? behavior}) {
if (iId != null) {
this._iId = iId;
}
if (breed != null) {
this._breed = breed;
}
if (origin != null) {
this._origin = origin;
}
if (url != null) {
this._url = url;
}
if (img != null) {
this._img = img;
}
if (breedInfo != null) {
this._breedInfo = breedInfo;
}
if (breedInfo1 != null) {
this._breedInfo1 = breedInfo1;
}
if (behavior != null) {
this._behavior = behavior;
}
}
Id? get iId => _iId;
set iId(Id? iId) => _iId = iId;
String? get breed => _breed;
set breed(String? breed) => _breed = breed;
String? get origin => _origin;
set origin(String? origin) => _origin = origin;
String? get url => _url;
set url(String? url) => _url = url;
String? get img => _img;
set img(String? img) => _img = img;
BreedInfo? get breedInfo => _breedInfo;
set breedInfo(BreedInfo? breedInfo) => _breedInfo = breedInfo;
BreedInfo? get breedInfo1 => _breedInfo1;
set breedInfo1(BreedInfo? breedInfo1) => _breedInfo1 = breedInfo1;
List<String>? get behavior => _behavior;
set behavior(List<String>? string) => _behavior = string;
DogClass.fromJson(Map<String, dynamic> json) {
_iId = json['_id'] != null ? Id.fromJson(json['_id']) : null;
_breed = json['breed'];
_origin = json['origin'];
_url = json['url'];
_img = json['img'];
_breedInfo = json['breed_info'] != null
? new BreedInfo.fromJson(json['breed_info'])
: null;
_breedInfo1 = json['breed_info1'] != null
? new BreedInfo.fromJson(json['breed_info1'])
: null;
if (json['behavior'] != null) {
List<dynamic> _behavior = [];
json['behavior'].forEach((v) {
_behavior.add(Behavior.fromJson(v));
});
}
}
}
class Id {
String? _oid;
Id({String? oid}) {
if (oid != null) {
_oid = oid;
}
}
String? get oid => _oid;
set oid(String? oid) => _oid = oid;
Id.fromJson(Map<String, dynamic> json) {
_oid = json['$oid'];
}
}
class BreedInfo {
String? _height;
String? _weight;
String? _coat;
List<String>? _imgSrcSet;
String? _lifeSpan;
String? _otherNames;
String? _commonNicknames;
String? _colour;
String? _litterSize;
String? _notes;
String? _breedStatus;
String? _foundationStock;
BreedInfo(
{String? height,
String? weight,
String? coat,
List<String>? imgSrcSet,
String? lifeSpan,
String? otherNames,
String? commonNicknames,
String? colour,
String? litterSize,
String? notes,
String? breedStatus,
String? foundationStock}) {
if (height != null) {
this._height = height;
}
if (weight != null) {
this._weight = weight;
}
if (coat != null) {
this._coat = coat;
}
if (imgSrcSet != null) {
this._imgSrcSet = imgSrcSet;
}
if (lifeSpan != null) {
this._lifeSpan = lifeSpan;
}
if (otherNames != null) {
this._otherNames = otherNames;
}
if (commonNicknames != null) {
this._commonNicknames = commonNicknames;
}
if (colour != null) {
this._colour = colour;
}
if (litterSize != null) {
this._litterSize = litterSize;
}
if (notes != null) {
this._notes = notes;
}
if (breedStatus != null) {
this._breedStatus = breedStatus;
}
if (foundationStock != null) {
this._foundationStock = foundationStock;
}
}
String? get height => _height;
set height(String? height) => _height = height;
String? get weight => _weight;
set weight(String? weight) => _weight = weight;
String? get coat => _coat;
set coat(String? coat) => _coat = coat;
List<String>? get imgSrcSet => _imgSrcSet;
set imgSrcSet(List<String>? imgSrcSet) => _imgSrcSet = imgSrcSet;
String? get lifeSpan => _lifeSpan;
set lifeSpan(String? lifeSpan) => _lifeSpan = lifeSpan;
String? get otherNames => _otherNames;
set otherNames(String? otherNames) => _otherNames = otherNames;
String? get commonNicknames => _commonNicknames;
set commonNicknames(String? commonNicknames) =>
_commonNicknames = commonNicknames;
String? get colour => _colour;
set colour(String? colour) => _colour = colour;
String? get litterSize => _litterSize;
set litterSize(String? litterSize) => _litterSize = litterSize;
String? get notes => _notes;
set notes(String? notes) => _notes = notes;
String? get breedStatus => _breedStatus;
set breedStatus(String? breedStatus) => _breedStatus = breedStatus;
String? get foundationStock => _foundationStock;
set foundationStock(String? foundationStock) =>
_foundationStock = foundationStock;
BreedInfo.fromJson(Map<String?, dynamic> json) {
_height = json['height'];
_weight = json['weight'];
_coat = json['coat'];
_lifeSpan = json['life_span'];
_otherNames = json['other_names'];
_commonNicknames = json['common_nicknames'];
_colour = json['colour'];
_litterSize = json['litter_size'];
_notes = json['notes'];
_breedStatus = json['breed_status'];
_foundationStock = json['foundation_stock'];
}
}
class Behavior {
Id? _iId;
String? _imageLink;
int? _goodWithChildren;
int? _goodWithOtherDogs;
int? _shedding;
int? _grooming;
int? _drooling;
int? _coatLength;
int? _goodWithStrangers;
int? _playfulness;
int? _protectiveness;
int? _trainability;
int? _energy;
int? _barking;
int? _minLifeExpectancy;
int? _maxLifeExpectancy;
double? _maxHeightMale;
double? _maxHeightFemale;
int? _maxWeightMale;
int? _maxWeightFemale;
int? _minHeightMale;
int? _minHeightFemale;
int? _minWeightMale;
int? _minWeightFemale;
String? _breed;
Behavior(
{Id? iId,
String? imageLink,
int? goodWithChildren,
int? goodWithOtherDogs,
int? shedding,
int? grooming,
int? drooling,
int? coatLength,
int? goodWithStrangers,
int? playfulness,
int? protectiveness,
int? trainability,
int? energy,
int? barking,
int? minLifeExpectancy,
int? maxLifeExpectancy,
double? maxHeightMale,
double? maxHeightFemale,
int? maxWeightMale,
int? maxWeightFemale,
int? minHeightMale,
int? minHeightFemale,
int? minWeightMale,
int? minWeightFemale,
String? breed}) {
if (iId != null) {
this._iId = iId;
}
if (imageLink != null) {
this._imageLink = imageLink;
}
if (goodWithChildren != null) {
this._goodWithChildren = goodWithChildren;
}
if (goodWithOtherDogs != null) {
this._goodWithOtherDogs = goodWithOtherDogs;
}
if (shedding != null) {
this._shedding = shedding;
}
if (grooming != null) {
this._grooming = grooming;
}
if (drooling != null) {
this._drooling = drooling;
}
if (coatLength != null) {
this._coatLength = coatLength;
}
if (goodWithStrangers != null) {
this._goodWithStrangers = goodWithStrangers;
}
if (playfulness != null) {
this._playfulness = playfulness;
}
if (protectiveness != null) {
this._protectiveness = protectiveness;
}
if (trainability != null) {
this._trainability = trainability;
}
if (energy != null) {
this._energy = energy;
}
if (barking != null) {
this._barking = barking;
}
if (minLifeExpectancy != null) {
this._minLifeExpectancy = minLifeExpectancy;
}
if (maxLifeExpectancy != null) {
this._maxLifeExpectancy = maxLifeExpectancy;
}
if (maxHeightMale != null) {
this._maxHeightMale = maxHeightMale;
}
if (maxHeightFemale != null) {
this._maxHeightFemale = maxHeightFemale;
}
if (maxWeightMale != null) {
this._maxWeightMale = maxWeightMale;
}
if (maxWeightFemale != null) {
this._maxWeightFemale = maxWeightFemale;
}
if (minHeightMale != null) {
this._minHeightMale = minHeightMale;
}
if (minHeightFemale != null) {
this._minHeightFemale = minHeightFemale;
}
if (minWeightMale != null) {
this._minWeightMale = minWeightMale;
}
if (minWeightFemale != null) {
this._minWeightFemale = minWeightFemale;
}
if (breed != null) {
this._breed = breed;
}
}
Id? get iId => _iId;
set iId(Id? iId) => _iId = iId;
String? get imageLink => _imageLink;
set imageLink(String? imageLink) => _imageLink = imageLink;
int? get goodWithChildren => _goodWithChildren;
set goodWithChildren(int? goodWithChildren) =>
_goodWithChildren = goodWithChildren;
int? get goodWithOtherDogs => _goodWithOtherDogs;
set goodWithOtherDogs(int? goodWithOtherDogs) =>
_goodWithOtherDogs = goodWithOtherDogs;
int? get shedding => _shedding;
set shedding(int? shedding) => _shedding = shedding;
int? get grooming => _grooming;
set grooming(int? grooming) => _grooming = grooming;
int? get drooling => _drooling;
set drooling(int? drooling) => _drooling = drooling;
int? get coatLength => _coatLength;
set coatLength(int? coatLength) => _coatLength = coatLength;
int? get goodWithStrangers => _goodWithStrangers;
set goodWithStrangers(int? goodWithStrangers) =>
_goodWithStrangers = goodWithStrangers;
int? get playfulness => _playfulness;
set playfulness(int? playfulness) => _playfulness = playfulness;
int? get protectiveness => _protectiveness;
set protectiveness(int? protectiveness) => _protectiveness = protectiveness;
int? get trainability => _trainability;
set trainability(int? trainability) => _trainability = trainability;
int? get energy => _energy;
set energy(int? energy) => _energy = energy;
int? get barking => _barking;
set barking(int? barking) => _barking = barking;
int? get minLifeExpectancy => _minLifeExpectancy;
set minLifeExpectancy(int? minLifeExpectancy) =>
_minLifeExpectancy = minLifeExpectancy;
int? get maxLifeExpectancy => _maxLifeExpectancy;
set maxLifeExpectancy(int? maxLifeExpectancy) =>
_maxLifeExpectancy = maxLifeExpectancy;
double? get maxHeightMale => _maxHeightMale;
set maxHeightMale(double? maxHeightMale) => _maxHeightMale = maxHeightMale;
double? get maxHeightFemale => _maxHeightFemale;
set maxHeightFemale(double? maxHeightFemale) =>
_maxHeightFemale = maxHeightFemale;
int? get maxWeightMale => _maxWeightMale;
set maxWeightMale(int? maxWeightMale) => _maxWeightMale = maxWeightMale;
int? get maxWeightFemale => _maxWeightFemale;
set maxWeightFemale(int? maxWeightFemale) =>
_maxWeightFemale = maxWeightFemale;
int? get minHeightMale => _minHeightMale;
set minHeightMale(int? minHeightMale) => _minHeightMale = minHeightMale;
int? get minHeightFemale => _minHeightFemale;
set minHeightFemale(int? minHeightFemale) =>
_minHeightFemale = minHeightFemale;
int? get minWeightMale => _minWeightMale;
set minWeightMale(int? minWeightMale) => _minWeightMale = minWeightMale;
int? get minWeightFemale => _minWeightFemale;
set minWeightFemale(int? minWeightFemale) =>
_minWeightFemale = minWeightFemale;
String? get breed => _breed;
set breed(String? breed) => _breed = breed;
Behavior.fromJson(Map<String, dynamic> json) {
_iId = json['_id'] != null ? new Id.fromJson(json['_id']) : null;
_imageLink = json['image_link'];
_goodWithChildren = json['good_with_children'];
_goodWithOtherDogs = json['good_with_other_dogs'];
_shedding = json['shedding'];
_grooming = json['grooming'];
_drooling = json['drooling'];
_coatLength = json['coat_length'];
_goodWithStrangers = json['good_with_strangers'];
_playfulness = json['playfulness'];
_protectiveness = json['protectiveness'];
_trainability = json['trainability'];
_energy = json['energy'];
_barking = json['barking'];
_minLifeExpectancy = json['min_life_expectancy'];
_maxLifeExpectancy = json['max_life_expectancy'];
_maxHeightMale = json['max_height_male'];
_maxHeightFemale = json['max_height_female'];
_maxWeightMale = json['max_weight_male'];
_maxWeightFemale = json['max_weight_female'];
_minHeightMale = json['min_height_male'];
_minHeightFemale = json['min_height_female'];
_minWeightMale = json['min_weight_male'];
_minWeightFemale = json['min_weight_female'];
_breed = json['breed'];
}
}
my original json:
{ "_id": { "$oid": "625f2900fe6aeb351381c3f3" }, "breed": "Affenpinscher", "origin": "Germany", "url": "https://en.wikipedia.org/wiki/Affenpinscher", "img": "https://upload.wikimedia.org/wikipedia/commons/thumb/1/17/Affenpinscher.jpg/220px-Affenpinscher.jpg", "breed_info": { "height": "23–30 cm (9–12 in)", "weight": "3–6 kg (7–13 lb)", "coat": "Wire", "img_src_set": [ null, { "5x": "https://upload.wikimedia.org/wikipedia/commons/thumb/1/17/Affenpinscher.jpg/330px-Affenpinscher.jpg" } ], "life_span": "Not available", "other_names": "Not available", "common_nicknames": "AffenAffie", "colour": "Not available", "litter_size": "Not available", "notes": "Not available", "breed_status": "Not available", "foundation_stock": "Not available" }, "breed_info1": { "height": "23–30 cm (9–12 in)", "weight": "3–6 kg (7–13 lb)", "coat": "Wire", "img_src_set": [ null, { "5x": "https://upload.wikimedia.org/wikipedia/commons/thumb/1/17/Affenpinscher.jpg/330px-Affenpinscher.jpg" } ], "life_span": "Not available", "other_names": "Not available", "common_nicknames": "AffenAffie", "colour": "Not available", "litter_size": "Not available", "notes": "Not available", "breed_status": "Not available", "foundation_stock": "Not available" }, "string": [ { "_id": { "$oid": "625f2ba9fe6aeb351381c647" }, "image_link": "https://api-ninjas.com/images/dogs/affenpinscher.jpg", "good_with_children": 3, "good_with_other_dogs": 3, "shedding": 3, "grooming": 3, "drooling": 1, "coat_length": 2, "good_with_strangers": 5, "playfulness": 3, "protectiveness": 3, "trainability": 3, "energy": 3, "barking": 3, "min_life_expectancy": 12, "max_life_expectancy": 15, "max_height_male": 11.5, "max_height_female": 11.5, "max_weight_male": 10, "max_weight_female": 10, "min_height_male": 9, "min_height_female": 9, "min_weight_male": 7, "min_weight_female": 7, "breed": "Affenpinscher" } ]}
and how I fetch the data:
List<dynamic> BreedList = [];
var client = http.Client();
fetchData(url) async {
final response = await client.get(Uri.parse(url));
if (response.statusCode == 200) {
var jsonDecoded = json.decode(response.body);
BreedList = jsonDecoded
.map((data) => DogClass.fromJson(data))
.toList();
return jsonDecoded;
} else {
throw Exception('Failed to load data');
}
}
CodePudding user response:
In your class BreedInfo List<String>? _imgSrcSet;
is wrong, it must be
List<Map<String, dynamic>>? _imgSrcSet
//or rather
List<Map<String, String>>? _imgSrcSet