hiranthaR / Json-to-Dart-Model

Json to Dart Model extension can convert JSON objects into Dart data classes. It supports pure Dart class conversion, Flutter-recommended JSON serialization using annotations, Freezed support, Effective Dart:Style, and many more features. Currently, it has more than 135,000 installs.
https://marketplace.visualstudio.com/items?itemName=hirantha.json-to-dart
MIT License
93 stars 17 forks source link

Make 0.0 as double not int when assigning it as a default value #92

Open globalwebforce opened 2 years ago

globalwebforce commented 2 years ago

Is there an existing issue for this?

Current Behavior

I have a rating property which has a default value of 0, but can have a value of 0.5, 2.4 etc.

{
  "d@rating": 0.00,
}

Expected Behavior

I was expecting to have a double but instead I received an int

@Default(0) int rating, //this must be double

Steps To Reproduce

No response

Version

No response

Relevant JSON syntax

No response

Anything else?

No response

iamarnas commented 2 years ago

@globalwebforce Hi.

I have tried to fix it a long time ago. But problem is that TypeScript calculate 0.0 as 0 becouse in reality is 0 for both double and integer. That why generator can't detect 0.0 as double becouse typescript always return 0. In your case you should write 0.01 as default and later correct in your code to 0.0 it is quick fix. And this issue describes in the documentation.

For example you can try another generator like QuickType and you will get same result.