when i run my app i get this error :
Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'ProductModel'
this is occurred when i use this method:
List<ProductModel> items2 = [];
FetchItems2() async {
Api ap1 = Api();
items2.add(ap1.GetProduct());
}
my Api class is :
class Api{
String BasicUrl = "my link";
var headers = {
'Authorization':
'some thing',
};
GetProduct() async {
final url = Uri.parse('${BasicUrl}products');
http.Response ProResponse = await http.get(url, headers: headers);
ProResponse.headers.addAll(headers);
List<ProductModel> items2 = [];
if (ProResponse.statusCode == 200) {
var decoded = (jsonDecode(ProResponse.body));
if (decoded.length == 0) {
print("null2");
} else {
for (var i in decoded) {
var images = (i["images"][0]["src"]);
var id = (i["id"]);
var description = (i["description"]);
var price = (i["price"]);
var regularprice = (i["regular_price"]);
var name = (i["name"]);
var productItem = ProductModel(
images: images ??=
"a link",
name: name,
description: description,
price: price ?? "",
regular_price: regularprice ?? "",
id: id);
items2.add(productItem);
}
}
return items2;
} else {
print(ProResponse.reasonPhrase);
return items2;
}
}
}
and finally my ProductModel class is:
class ProductModel{
late final int id;
late final String name;
late final String regular_price;
late final String price;
late final String description;
late final String images;
ProductModel({ required this.id,required this.name,
required this.regular_price,
required this.price,
required this.description,required this.images});
factory ProductModel.fromJson(Map<String,dynamic> json){
final name = json["name"];
final id = json ["id"];
final regular_price = json["regular_price"];
final price = json["price"];
final description = json["description"];
final images = json["images"];
return ProductModel(id: id, name: name,
regular_price: regular_price,
price: price,
description: description, images: images);
}
}
console says GetProduct method is dynamic and it should be the type of ProductModel and i don't know how to fix it.
note : also i tried this:
List<ProductModel> GetProduct() async {
but it doesn't work. Could someone please help me please ? it's so important and i have to fix it.
CodePudding user response:
Your async
functions should always return a Future
. So in this instance you don't want
ProductModel GetProduct() async {}
but rather
Future<ProductModel> getProduct() async {}
And then you can load the products and add them to the list
final items2 = <ProductModel>[];
Future<void> FetchItems2() async {
final ap1 = Api();
final product = await ap1.getProduct();
items2.add(product);
}
CodePudding user response:
Actually, You don't have a method type by default it will Future dynamic when it's async
- First, we will change it to Future of ProductModel
- Second, You used for loop for getting a list of data to ProductModel it is not a good practice, use the following code for returning List of ProductModel
Future GetProduct() async { final url = Uri.parse('${BasicUrl}products');
http.Response ProResponse = await http.get(url, headers: headers);
ProResponse.headers.addAll(headers);
if (ProResponse.statusCode == 200) {
var decoded = jsonDecode(ProResponse.body);
if (decoded.length == 0) {
print("null2");
} else {
return decoded .map((user) => ProductModel.fromJson(user)).toList();
}
}
//return items2;
} else {
print(ProResponse.reasonPhrase);
// return items2;
}
}