I am a beginner in Flutter, I just want to know how to print productA and 220 in console. The below is json file and also create dart file below.
{
"status": true,
"message": "Data returned successfull",
"data": {
"products": [
{
"productName": "productA",
"productPrice": "220.00",
}
]
}
}
Product.dart
class Product {
bool status;
String message;
Data data;
Product({this.status, this.message, this.data});
Product.fromJson(Map<String, dynamic> json) {
status = json['status'];
message = json['message'];
data = json['data'] != null ? new Data.fromJson(json['data']) : null;
}
}
class Data {
List<Products> products;
Data({this.products});
Data.fromJson(Map<String, dynamic> json) {
if (json['products'] != null) {
products = new List<Products>();
json['products'].forEach((v) {
products.add(new Products.fromJson(v));
});
}
}
}
class Products {
String prodName;
String prodRkPrice;
Products({this.prodName, this.prodRkPrice});
Products.fromJson(Map<String, dynamic> json) {
prodName = json['prodName'];
prodRkPrice = json['prodRkPrice'];
}
}
But still don't know how to print those values.
fetchData() async {
try {
String extractedData =
await http.get('json url').toString();
final parsed = jsonDecode(extractedData).cast(Map<String, dynamic>());
final products = Product.fromJson(parsed);
print(products);
//print(json.decode(response.body));
//print(response[0]);
} catch (error) {
throw (error);
}
}
I have tried using this method but getting errors, don't know how to parse and print those values?
Please help me I am a beginner in Flutter
question from:
https://stackoverflow.com/questions/65598298/convert-json-to-dart 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…