简体   繁体   English

在Flutter / Dart中解析列表

[英]Parsing a List in Flutter / Dart

I've a list that looks like this: 我有一个看起来像这样的列表:

[{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}]

So it's basically 3 objects. 因此,基本上是3个对象。

The object model looks like this: 对象模型如下所示:

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;
  }
}

I can't seem to figure out how to parse this list. 我似乎无法弄清楚如何解析此列表。

If it's just a single object that's being returned by my API I run it through this function: 如果我的API返回的只是一个对象,则可以通过以下函数运行它:

currentChallenge = Challenge.fromJson(response.data);

How to do something similar but then end up with a UploadedChallengesList instead of a single object? 如何做类似的事情,但最终得到一个UploadedChallengesList而不是单个对象?

I've tried: 我试过了:

challengeUploads = json.decode(response.data).map<ChallengeUpload>((dynamic challengeUpload) => ChallengeUpload.fromJson(challengeUpload)).toList();

It'll print: I/flutter ( 2660): type 'List<dynamic>' is not a subtype of type 'String' 它将显示: I/flutter ( 2660): type 'List<dynamic>' is not a subtype of type 'String'

My JSON: 我的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
}

Edit: I found out it had to do with the library I'm using to do API calls. 编辑:我发现它与我用来进行API调用的库有关。 I use Dio and my code ended up looking like this: 我使用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();
  });

How about this? 这个怎么样?

List<Map<String, dynamic>> jsonList = json.decode(response.data.toString()) as List;

List<ChallengeUpload> myList = jsonList.map(
    (jsonElement) => ChallengeUpload.fromJson(jsonElement)
).toList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM