Closed alexrosabr closed 2 months ago
You need to use a converter. See https://pub.dev/packages/json_serializable#custom-types-and-custom-encoding
Thanks! For reference, here's my DecimalConverter
:
class DecimalConverter implements JsonConverter<Decimal?, dynamic> {
const DecimalConverter();
@override
Decimal? fromJson(dynamic json) => Decimal.tryParse('$json');
@override
double? toJson(Decimal? object) => object?.toDouble();
}
I had to use dynamic
-- the API may return {"multiplier":0}
sometimes.
FromJson
The generated code uses
as String
but when the data comes from the API it fails with:If I edit the generated code to use
.toString()
instead, it works.ToJson
The generated code does not work with SQLite; the warning message says it will allow it for now but the warning may become an error in the future.
If I edit the generated code to use
?.toDouble()
it works.Edited code (to make it work)
Is there a way to configure "json_serialize" to generate code like this?