我在flutter中使用fromJson时遇到问题。我正在接收这样的数据
{id: d7ba912e-69fd-11ea-9ab0-6597c4120b03, receipt_date: 2020-03-18T17:30:00.000Z, customer_id: e3fedf5e, amount: 2500, remark: Amount 2500, created_on: 2020-03-19T16:22:35.000Z, collectionSize: 3, name: Customer 1}
而且我正在使用fromJson进行解析:
factory CashReceipt.fromJson(Map<String, dynamic> json) {
return CashReceipt(
id: json['id'],
customerId: json['customer_id'],
date: json['date'] as DateTime,
amount: json['amount'] as double,
remark: json['remark']);
}
“ amount”属性在我的模型中是两倍。这导致以下错误。来自API的数据为“金额:2500”。如果我将“金额”更改为“ int”,它将消失。但这是不对的。非常感谢您的帮助。
_CastError (type 'int' is not a subtype of type 'double' in type cast)