我使用dart包json_serializable进行json序列化.查看flutter文档,它显示了如何反序列化单个对象,如下所示:
Future<Post> fetchPost() async {
final response =
await http.get('https://jsonplaceholder.typicode.com/posts/1');
if (response.statusCode == 200) {
// If the call to the server was successful, parse the JSON
return Post.fromJson(json.decode(response.body));
} else {
// If that call was not successful, throw an error.
throw Exception('Failed to load post');
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我对dart不太熟悉,无法弄清楚如何对项目列表而不是单个实例执行相同的操作.