【发布时间】:2021-10-11 00:33:00
【问题描述】:
所以我正在使用来自 rest 服务的这个 API,它以以下 json 格式向我发送数据。
API 响应
"data": [
{
"id": 3,
"title": "Lorem Ipsum Post",
"excerpt": "This is the excerpt for the Lorem Ipsum Post",
"body": "<p>This is the body of the lorem ipsum post</p>",
"created_at": "2021-08-03T11:54:52.000000Z",
"updated_at": "2021-08-03T11:54:52.000000Z"
},
{
"id": 4,
"title": "My Sample Post",
"excerpt": "This is the excerpt for the sample Post",
"body": "<p>This is the body for the sample post, which includes the body.</p>\n <h2>We can use all kinds of format!</h2>\n <p>And include a bunch of other stuff.</p>",
"created_at": "2021-08-03T11:54:52.000000Z",
"updated_at": "2021-08-03T11:54:52.000000Z"
},
]
我使用以下方法将来自 api 响应的值插入到我以后需要使用的列表中。
我使用的方法
Future<List<Post>> getAllPosts(String token) async {
final ApiResponse apiResponse = await _apiBaseHelper.get(
"https://test.sugayo.com/api/cg/posts/limit/3",token);
Post allPost=Post.fromJson(jsonDecode(apiResponse.data));
List<Post> allPostList=[Post(id: allPost.id, subtitle: allPost.subtitle, secondaryText: allPost.secondaryText, body: allPost.body)];
return allPostList;
}
在终端中我收到此错误
错误:需要一个“Map
【问题讨论】:
-
在此处尝试我的答案,以从此处stackoverflow.com/a/68533647/13997210 或此处stackoverflow.com/a/68594656/13997210 的 API 获取数据,希望对您有所帮助
-
请添加完整的 API 响应。 “数据”块是否用大括号括起来?
Post.fromJson()将什么作为输入。请在问题中也添加该方法。