I was using dio to get the API response from http://fakestoreapi.com/docs . While i was getting the response , which is a List of Maps (sample json output at https://pastebin.com/ubkJLp7d ), it is when I am trying to convert this list into a List of Product (my PODO) that I am running into an error. i made a function :
final ApiService _api = locator<ApiService>();
Future<List<Product>> getProducts() async {
final response = await _api.get(endpoint: 'products?limit=5');
try {
List<Product> products =
List<Product>.from(response.map((e) => Product.fromJson(e)));
return products;
} on Exception catch (e) {
print(e.toString());
}
return [];
}
When I am using this function in a FutureBuilder, I am snapshot.hasData=false. Just to check the response , I made a test function :
Future<void> test() async {
final response = await _api.get(endpoint: 'products?limit=5');
print(response.length);
print(response[0]);
print(Product.fromJson(response[0]).title);
}
And it gave all the right outputs. So I making some mistake while making the List of Products. What am I doing wrong and how to fix it?
Relavant code :
product.dart:
class Product {
int id, rateCount;
double price, rating;
String title, description, category, imageUrl;
Product({
required this.id,
required this.rateCount,
required this.price,
required this.rating,
required this.title,
required this.description,
required this.category,
required this.imageUrl,
});
factory Product.fromJson(Map<String, dynamic> json) {
return Product(
id: json['id'],
rateCount: json['rating']['count'],
price: json['price'],
rating: json['rating']['rate'],
title: json['title'],
description: json['description'],
category: json['category'],
imageUrl: json['image'],
);
}
Map<String, dynamic> toJson() => {
'id': id,
'title': title,
'price': price,
'description': description,
'category': category,
'image': imageUrl,
'rating': {
'rate': rating,
'count': rateCount,
}
};
}
api_service.dart:
class ApiService {
static const String BASE_URL = 'https://fakestoreapi.com/';
final Dio _dio = Dio();
Future<dynamic> get({required String endpoint}) async {
String url = BASE_URL + endpoint;
try {
final response = await _dio.get(url);
if (response.statusCode! < 205) {
return response.data!;
}
} on Exception catch (e) {
print(e.toString());
}
return [];
}
}
getProducts()as a future for a FutureBuilder, in the builder , its givingsnapshot.hasData=false. I am getting the response from API, its when I am trying to convert it into a list, I am hitting a snag.