代码之家  ›  专栏  ›  技术社区  ›  Dolphin

为什么将地图数据解析为对象跳转

  •  0
  • Dolphin  · 技术社区  · 4 年前

    我使用这段代码将散列映射数据解析为flatter中的一个对象:

    factory Channel.fromMap(Map<String, dynamic> json) => Channel(
        id: json["id"],
        author: json["author"] == null ? "" : json["author"],
        deleted: json["deleted"] == null ? false : json["deleted"],
        content: json["content"] == null ? "" : json["content"],
        dead: json["dead"] == null ? false : json["dead"],
        poll: json["poll"] == null ? null : json["poll"],
        parent: json["parent"] == null ? null : json["parent"],
        parts: json["parts"] == null
            ? []
            : List<int>.from(json["parts"].map((x) => x)),
        descendants: json["descendants"] == null ? 0 : json["descendants"],
        kids: json["kids"] == null
            ? []
            : List<int>.from(json["kids"].map((x) => x)),
        score: json["score"] == null ? 0 : json["score"],
        pubTime: json["pubTime"] == null ? 0 : json["pubTime"],
        title: json["title"] == null ? "" : json["title"],
        subName: json["subName"] == null?"":json["subName"],
        subUrl: json["subUrl"] == null?"":json["subUrl"],
        isFav: json["isFav"] == null?"":json["isFav"],
        intro: json["intro"] == null?"":json["intro"],
      );
    

    当代码运行到此行时 isFav: json["isFav"] == null?"":json["isFav"], 对于代码,只需跳转并不返回任何内容,没有错误输出,我应该怎么做来解决这个问题?

    enter image description here

    static List<Channel> convertTheResult(Response response) {
        if (RestClient.respSuccess(response)) {
          var channelResult = response.data["result"];
          if (channelResult == null) {
            return null;
          }
          var channelListResult = channelResult["list"];
          List<Channel> channels = [];
          channelListResult.forEach((element) {
            try {
              Channel parsedChannel = Channel.fromMap(element);
              if (parsedChannel != null) {
                channels.add(parsedChannel);
              }
            } on Exception catch (e) {
              CruiseLogHandler.logError(CruiseApiError('Channel parsed failed.'), JsonEncoder().convert(response));
            }
          });
          return channels;
        }
      }
    

    仍然没有捕捉到任何异常线索。

    0 回复  |  直到 4 年前
        1
  •  0
  •   Dolphin    4 年前

    这个 isFav 数据类型为int,更改代码如下:

        isFav: json["isFav"] == null ? 0 : json["isFav"],