Json的Dart无法解析双精度值

问题描述 投票:0回答:1

我在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)
dart fromjson
1个回答
0
投票
您可以使用

num amount

代替

double amount

并且您不会收到此错误。看到这个doc
© www.soinside.com 2019 - 2024. All rights reserved.