What's new

How to deserialize Data ISO String to DateTime Object in built_value serialization in dart?


Active member
Staff member
I wanna serialize a json object including an ISOString date to a dart object using built value.

this is a sample json:

"title": "test",
"description": "test description",
"date": "2020-06-05T11:42:38.585Z",
"creator": {
"email": "test@test.com"

this is the model:

abstract class Post implements Built<Post, PostBuilder> {
@BuiltValueField(wireName: '_id')
String get id;

String get title;

String get description;

DateTime get date;

User get creator;


static Serializer<Post> get serializer => _$postSerializer;

factory Post([updates(PostBuilder b)]) = _$Post;

factory Post.fromJson(Map<String, dynamic> map) =>
serializers.deserializeWith(Post.serializer, map);

Map<String, dynamic> toJson() =>
serializers.serializeWith(Post.serializer, this);

and this is the error:

Deserializing '[title, test1, description, test1 description, date, 2020-06-05T...' to 'Post' failed
due to: Deserializing '2020-06-05T11:42:38.585Z' to 'DateTime' failed due to: type 'String' is not a
subtype of type 'int' in type cast

how do I fix that?

Continue reading...