简体   繁体   中英

Dart fromJson cannot parse double values

I'm having problem using fromJson in flutter. I'm receiving my data like this

{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}

And I'm using this fromJson to parse:

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']); }

The "amount" property is double in my model. It is causing following error. The data coming from API is "amount:2500". It goes away if I change the "amount" to "int". But it is not right. Your help is much appreciated.

_CastError (type 'int' is not a subtype of type 'double' in type cast)

You can use

num amount

instead of

double amount

and you will not get this error. see this doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM