This question is related to this post.
I tried the following code:
import 'dart:convert';
/*server side Post class */
class Post {
int post_id;
String title;
String description;
DateTime posted_at;
DateTime last_edited;
String user;
String editor;
int up_votes;
int down_votes;
int total_votes;
String links_to;
List<String> tags = new List();
Post.fromSQL(List sql_post) {
//initialization code, unrelated.
}
Map toJson(){
Map fromObject = {
'post_id' : post_id,
'title' : title,
'description' : description,
'posted_at' : posted_at,
'last_edited' : last_edited,
'user' : user,
'editor' : editor,
'up_votes' : up_votes,
'dwon_votes' : down_votes,
'total_votes' : total_votes,
'links_to' : links_to,
'tags' : tags
};
return fromObject;
//I use the code below as a temporary solution
//JSON.encode(fromObject, toEncodable: (date)=>date.toString());
}
}
I have a temporary solution, but I would really like to be able to do the following.
JSON.encode(posts, toEncodable: (date)=>date.toString())
where posts
is a list of Post objects. I would expect this to convert into a json list of json representations of the Post class. What I get is a list of "Instance of 'Post'"
strings.
So the question is, is this syntax no longer supported, or should I do something differently?