【问题标题】:Parsing a List in Flutter / Dart在 Flutter / Dart 中解析列表
【发布时间】: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&lt;ChallengeUpload&gt;((dynamic challengeUpload) =&gt; ChallengeUpload.fromJson(challengeUpload)).toList();

它会打印: I/flutter ( 2660): type 'List&lt;dynamic&gt;' 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 响应

标签: json flutter dart


【解决方案1】:

这个怎么样?

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

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

【讨论】:

  • sn-p 将导致以下结果:I/flutter (2660): NoSuchMethodError: The method 'map' was called on null.
  • 是的,那是因为jsonList需要从你的json字符串初始化
  • 谢谢,它似乎(几乎工作)。我需要将 response.data 更改为 response.data.toString()
猜你喜欢
  • 2020-11-25
  • 2020-11-29
  • 2021-02-09
  • 2021-07-10
  • 2022-01-09
  • 2021-05-17
  • 2021-07-02
  • 2020-07-06
  • 2020-05-30
相关资源
最近更新 更多