How to encode an object to json in Flutter
jsonEncode requires a Map<String, dynamic>
, not a Week
object. Calling your toJson()
method should do the trick.
var json = jsonEncode(week.toJson());
However, keep in mind that your toJson()
method is also incorrect, as things like _goalList and the dates are still objects, not Maps or Lists. You'll need to implement toJson methods on those as well.
To answer your specific questions:
- Because dart is not javascript / typescript. Dart checks types at runtime, therefore you have to explicitly tell it how to convert things - also there is no reflection in dart, so it can't figure it out by itself.
- You can use a library that uses code generation to do these things automatically for you - it still wont be possible at runtime though - read more about JSON serialization.
- The easiest way would be to implement the methods directly in the classes, as that's where you have access to in your root object. Keep in mind that the structure that
jsonEncode
needs is aMap<String, dynamic>
, but thedynamic
part really meansList<dynamic>
,Map<String, dynamic>
or a primitive that is json compatible such asString
ordouble
- if you try to imagine how such a nested structure of said types looks, you'll realise that it's basically json. So when you do something like'goalList': _goalList,
you're giving it an object, which is not one of the allowed types.
Hope this clears things up a bit.
for anyone wondering: I got my solution.
To make my code work I needed to implement the toJson()
methods at my class Goal
as well (because I used List<Goal>
in Week
).
class Goal{
String _text;
bool _reached;
Map<String, dynamic> toJson() =>
{
'text': _text,
'reached': _reached,
};
}
Also, I needed to add .toIso8601String()
to the DateTime
objects like that in the Week
class:
Map<String, dynamic> toJson() =>
{
'startDate': _startDate.toIso8601String(),
'endDate': _endDate.toIso8601String(),
'goalList': _goalList,
'improvement': _improvement,
};
Now the output is:
{"startDate":"2019-05-27T00:00:00.000Z","endDate":"2019-06-02T00:00:00.000Z","goalList":[],"improvement":""}