0

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)

1 Answer 1

1

You can use

num amount

instead of

double amount

and you will not get this error. see this doc

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.