I was trying to save from API call List of Pets which contain String values and Pets Image URL list but when I try to save to sqflite database I got an error
Invalid argument [Instance of 'Photo'] with type List.
response from postman looks like below
"pets": [
{
"type": "Cat",
"gender": "male",
"size": "medium",
"age": "baby",
"photos": [
{
"url": null
}
],
"good_with_children": true,
"pet_id": "63fdb1b5cfaa63a48ed4a48e",
"source": "local"
},
]
here is my model
class PetsModel extends Pets{
PetsModel(
{
required super.type,
required super.gender,
required super.size,
required super.age,
required super.photos,
required super.good_with_children,
required super.source});
factory PetsModel.fromJson(Map<String, dynamic> json)=>
PetsModel(type:json['type'],
gender:json['gender'],
size:json['size'],
age:json['age'],
photos:List<Photo>.from(json['photos'].map((x) => Photo.fromJson(x))),
good_with_children:json['good_with_children'],
source:json['source']);
Map<String, dynamic> toJson() => {
"type": type,
"gender": gender,
"size": size,
"age": age,
"photos": photos,
"good_with_children": good_with_children,
"source": source,
};
Photos Model
class Photo {
Photo({
required this.url,
});
String? url;
factory Photo.fromJson(Map<String, dynamic> json) => Photo(
url: json["url"]??'',
);
}
so I have create a sqlite database to store the response for offline usage and to save the value i used the following
createPets(PetsModel newPetsList) async {
final db = await database;
final res = await db!.insert('pets', newPetsList.toJson());
return res;
}
so the main cause of error is The Photo List and I have seen different solutions I couldn't solve it out. any help is appreciated