这个问题与这篇文章有关。
我尝试了以下代码:
我尝试了以下代码:
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());
}
}
我有一个临时解决方案,但我真的希望能够做到以下:
JSON.encode(posts, toEncodable: (date)=>date.toString())
在这里,posts
是一个帖子对象列表。我期望它可以转换成一个 Post 类的 JSON 表示形式列表。但实际得到的是一个由字符串“Instance of 'Post'”组成的列表。
那么问题来了,是语法不再支持,还是我该采取其他策略呢?
toEncodable
是在toJson()返回的对象的那些不可编码的子元素上运行的。最终,我通过稍微更改Map来解决了这个问题:'posted_at' : posted_at.toIso8601String(), 'last_edited' : last_edited.toIso8601String()
,并完全删除了toEncodable
。 - LukasztoEncodable
函数是要使用的函数,没有其他无法编码对象的备选方案。如果您没有提供另一个函数,则toJson
调用只是默认的toEncodable
函数。 - lrn