【发布时间】:2019-10-26 20:09:26
【问题描述】:
我有一个如下所示的列表:
[{id: 1, user_id: 3, challenge_id: 1, created_at: 2019-06-09 06:36:39, image_caption: Enter your image caption here, image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560062199/zex61jegvqwkevq6qrmd.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0}, {id: 2, user_id: 2, challenge_id: 1, created_at: 2019-06-12 09:17:07, image_caption: , image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560331027/dj94sjzufx8gznyxrves.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0},
{id: 2, user_id: 3, challenge_id: 1, created_at: 2019-06-09 06:36:39, image_caption: Enter your image caption here, image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560062199/zex61jegvqwkevq6qrmd.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0}, {id: 2, user_id: 2, challenge_id: 1, created_at: 2019-06-12 09:17:07, image_caption: , image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560331027/dj94sjzufx8gznyxrves.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0},
{id: 3, user_id: 3, challenge_id: 1, created_at: 2019-06-09 06:36:39, image_caption: Enter your image caption here, image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560062199/zex61jegvqwkevq6qrmd.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0}, {id: 2, user_id: 2, challenge_id: 1, created_at: 2019-06-12 09:17:07, image_caption: , image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560331027/dj94sjzufx8gznyxrves.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0}]
所以它基本上是 3 个对象。
对象模型如下所示:
class ChallengeUpload {
int id = 0;
int userId = 0;
int challengeId = 0;
String createdAt = "";
String imageCaption = "";
String imagePath = "";
File image;
String userUpvoted = "";
String userDownvoted = "";
int score = 0;
ChallengeUpload();
ChallengeUpload.fromJson(Map<String, dynamic> json) {
id = json['id'];
userId = json['user_id'];
challengeId = json['challenge_id'];
createdAt = json['created_at'];
imageCaption = json['image_caption'];
imagePath = json['image_path'];
userUpvoted = json['user_upvoted'];
userDownvoted = json['user_downvoted'];
score = json['score'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['user_id'] = this.userId;
data['challenge_id'] = this.challengeId;
data['created_at'] = this.createdAt;
data['image_caption'] = this.imageCaption;
data['image_path'] = this.imagePath;
data['user_upvoted'] = this.userUpvoted;
data['user_downvoted'] = this.userDownvoted;
data['score'] = this.score;
return data;
}
}
我似乎不知道如何解析这个列表。
如果它只是我的 API 返回的单个对象,我会通过这个函数运行它:
currentChallenge = Challenge.fromJson(response.data);
如何做类似的事情,但最终得到一个 UploadedChallengesList 而不是单个对象?
我试过了:
challengeUploads = json.decode(response.data).map<ChallengeUpload>((dynamic challengeUpload) => ChallengeUpload.fromJson(challengeUpload)).toList();
它会打印:
I/flutter ( 2660): type 'List<dynamic>' is not a subtype of type 'String'
我的 JSON:
{
id:1,
user_id:3,
challenge_id:1,
created_at:2019-06 -09 06:36:39,
image_caption:Enter your image caption here,
image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560062199/zex61jegvqwkevq6qrmd.jpg,
image:null,
user_upvoted:null,
user_downvoted:null,
score:0
},
{
id:2,
user_id:2,
challenge_id:1,
created_at:2019-06 -12 09:17:07,
image_caption:,
image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560331027/dj94sjzufx8gznyxrves.jpg,
image:null,
user_upvoted:null,
user_downvoted:null,
score:0
}
编辑:我发现它与我用来执行 API 调用的库有关。我使用 Dio,我的代码最终看起来像这样:
Response response = await Dio().get(url,
options: Options(
headers: {"Authorization": accessToken},
responseType: ResponseType.json));
setState(() {
challengeUploads = response.data
.map<ChallengeUpload>((dynamic challengeUpload) =>
ChallengeUpload.fromJson(challengeUpload))
.toList();
});
【问题讨论】:
-
除了
response.data似乎是 List而不是 json.decode所期望的 String 之外,您尝试过的应该可以工作。response的类型是什么? -
发布您的 json 响应