How it be happend ? i think it happend when i doin json mapping. it said Exception: type 'int' is not a subtype of type 'String'. I've tried using local json assets file but it not make different. Please help me with this problem.
class Product {
int productId;
String productName;
String productPrice;
Product({this.productId, this.productName, this.productPrice});
Product.fromProduct(Product p) {
this.productId = p.productId;
this.productName = p.productName;
this.productPrice = p.productPrice;
}
factory Product.fromJson(Map<String, dynamic> parsedJson) {
return Product(
productId: parsedJson['ID'],
productName: parsedJson['Name'],
productPrice: parsedJson['SellPrice']);
}
}
Future<String> _loadAProductAsset() async {
var res = await http.get(Uri.encodeFull("http://10.0.2.2:9155/product"));
return json.encode(json.decode(res.body)["data"]);
}
List<Product> parseProduct(String myJson) {
final parsed = json.decode(myJson).cast<Map<String, dynamic>>();
return parsed.map<Product>((json) => Product.fromJson(json)).toList();
}
Future<List<Product>> fetchProduct() async {
await wait(1);
String jsonString = await _loadAProductAsset();
return compute(parseProduct, jsonString);
}
Future wait(int s) {
return new Future.delayed(Duration(seconds: s), () => {});
}
this is my json from _loadAProductAsset() function
[
{
"ID": 2,
"CreatedAt": "2020-01-06T03:56:32+07:00",
"UpdatedAt": "2020-01-06T03:56:32+07:00",
"DeletedAt": null,
"Name": "Product A",
"Category": "0",
"Stock": "50",
"StockUnit": "0",
"BuyPrice": "20000",
"SellPrice": "21000",
"SupplierID": "1"
}
]