I can decode response from server but i don't know what is this error for :\
here's my request code in showproduct class:
if (response.statusCode == 200) {
setState(() {
var bodybytedecoded= jsonDecode(utf8.decode(response.bodyBytes));
final product= ProductModel.fromJson(bodybytedecoded); // console shows error come from this line
print(product);
}
});
}
else {
print(response.reasonPhrase);
}
In my Model class i have :
class ProductModel{
final int id;
final String name;
final String regular_price;
final String description;
final List<ImageModel> image;
ProductModel({ required this.id,required this.name,required this.regular_price, required this.description,required this.image});
factory ProductModel.fromJson(Map<String,dynamic> json){
final name = json["name"];
final id = json ["id"];
final regular_price = json["regular_price"];
final description = json["description"];
final image = json["image"];
return ProductModel(id: id, name: name, regular_price: regular_price, description: description, image: image);
}
Map<String,dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data["id"] = this.id;
data["name"] = this.name;
data["regular_rice"] = this.regular_price;
data["description"] = this.description;
data["image"] = this.image;
return data;
}
}
class ImageModel{
final String src;
ImageModel({required this.src});
factory ImageModel.fromJson(Map<String,dynamic> json ){
return ImageModel(src: json["src"]);
}
}
i don't know what is this error for, is there any wrong with my model class? or my request? i will be grateful if someone could show me a true way.
here's full error:
E/flutter (31338): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
E/flutter (31338): #0 _ShowProductState.FetchItems.<anonymous closure> (package:mystore/ProductPages/showproduct.dart:219:46)
E/flutter (31338): #1 State.setState (package:flutter/src/widgets/framework.dart:1088:30)
E/flutter (31338): #2 _ShowProductState.FetchItems (package:mystore/ProductPages/showproduct.dart:215:7)
E/flutter (31338): <asynchronous suspension>
E/flutter (31338):
and here's the json body from server:
[
{
"id": 56012,
"name": "a name",
"slug": "a slug",
"description": "<p>آب آشامیدنی پیورلایف 1/5 لیتری نستله</p>\n",
"regular_price": "45000",
"sale_price": "45000",
"tags": [],
"images": [
{
"id": 56043,
"src": "pic link",
"name": "A name",
}
],
},
]
CodePudding user response:
add this line to your ProductModel
List<ProductModel> productFromJson(String str) => List<ProductModel>.from(jsonDecode(str).map((x) => ProductModel.fromJson(x)));
and change your code to like this
if (response.statusCode == 200) {
setState(() {
var bodybytedecoded= jsonDecode(utf8.decode(response.bodyBytes));
final product= productFromJson(bodybytedecoded); // console shows error come from this line
print(product);
}
});
}
else {
print(response.reasonPhrase);
}
CodePudding user response:
The problem is you are trying to Map a List to a Map change this line to
final product= ProductModel.fromJson(bodybytedecoded);
to
var bodybytedecoded= jsonDecode(utf8.decode(response.bodyBytes));
final List<ProductModel> products = bodybytedecoded
.map((model) => ProductModel.fromJson(model));
On a side note there is a typo in toJson
data["regular_rice"] = this.regular_price;
CodePudding user response:
You can simply do this by decoding your data and storing the value in the list.
var decodedResponse= jsonDecode(resonse.body);
List<ProductModel> items=[];
decodedResponse.forEach((e){
items.add(ProductModel.fromJson(e));
});