Seeking your help.
Actually, I've created an object model class. However, when I tried to parse the JSON response to the object model but error as below :
JSON Response
"value": [
{
"CardCode": "C20000",
"CardName": "Maxi Teq",
"DocCur": "AUD",
"DocEntry": 793,
"DocNum": 793,
"DocTotal": 99.0,
"DocType": "I",
"U_Driver": "addon",
"U_GLINK": "https://goo.gl/maps/tQJh7Zj9fpUzQqcv9"
},
{
"CardCode": "C20000",
"CardName": "Maxi Teq",
"DocCur": "AUD",
"DocEntry": 795,
"DocNum": 795,
"DocTotal": 99.0,
"DocType": "I",
"U_Driver": "addon",
"U_GLINK": "https://goo.gl/maps/tQJh7Zj9fpUzQqcv9"
}
]
Model All the variables are string, but the JSON response includes int.
import 'package:json_annotation/json_annotation.dart';
part 'order.g.dart';
@JsonSerializable(explicitToJson: true)
class order {
String? cardCode;
String? cardName;
String? docCur;
String? docEntry;
String? docNum;
String? docTotal;
String? docType;
String? uDriver;
String? uGLINK;
order({this.cardCode, this.cardName, this.docCur, this.docEntry, this.docNum, this.docTotal, this.docType, this.uDriver, this.uGLINK});
factory order.fromJson(Map<String,dynamic> data) => _$orderFromJson(data);
Map<String,dynamic> toJson() => _$orderToJson(this);
}
Main Class
Map<String, dynamic> orderMap = json.decode(orderJson);
List<dynamic> orderList = orderMap["value"];
print("Method-------------$orderList");
List<order> list = orderList.map((val) => order.fromJson(val)).toList();
print(list.toString());
Error Response
I/flutter (26244): [Instance of 'order', Instance of 'order', Instance of 'order', Instance of 'order', Instance of 'order']
Thank you
CodePudding user response:
That is not an error. That is the result of: print(list.toString());
What you are lacking is a toString()
method on the class order
, so it just prints that you have a list of instances of type order
.
Add something like this to your order
class:
@override
String toString() {
return 'order(cardCode: $cardCode, cardName: $cardName, docCur: $docCur, docEntry: $docEntry, docNum: $docNum, docTotal: $docTotal, docType: $docType, uDriver: $uDriver, uGLINK: $uGLINK)';
}
CodePudding user response:
Use orderList.elementAt(0) to get a map of value.