json - 未处理的异常 : type '(dynamic) => Welcome' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform'

标签 json api dart unhandled-exception jsonparser

我正在尝试从 json 到 dart 的数据序列化。

但我没有发现这个错误无法克服。

未处理的异常:类型“(dynamic)=>Welcome”不是“transform”类型“(String,dynamic)=>MapEntry”的子类型

我会显示我的代码,有帮助的人可以回答我。

此代码的错误来源为Android Studio。 我在这里收到错误:

void getTracksFromApi() {
    PlayListApi.getTracks().then((response) {
      setState(() {
        var list = json.decode(response.body);
        this.trackList = list.map((track) => Welcome.fromJson(track)).toList();
        var sa = list["tracks"];
        print('$sa');
      });
    });
  }

也许问题出在我的模型上。 我用这两个工具构建https://app.quicktype.iohttps://javiercbk.github.io/json_to_dart/ 模型在这里:

import 'dart:convert';

Welcome welcomeFromJson(String str) => Welcome.fromJson(json.decode(str));

String welcomeToJson(Welcome data) => json.encode(data.toJson());

class Welcome {
  Welcome({
    this.tracks,
  });

  final Tracks tracks;

  factory Welcome.fromJson(Map<String, dynamic> json) => Welcome(
        tracks: json["tracks"] == null ? null : Tracks.fromJson(json["tracks"]),
      );

  Map<String, dynamic> toJson() => {
        "tracks": tracks == null ? null : tracks.toJson(),
      };
}

class Tracks {
  Tracks({
    this.items,
  });

  final List<Item> items;

  factory Tracks.fromJson(Map<String, dynamic> json) => Tracks(
        items: json["items"] == null
            ? null
            : List<Item>.from(json["items"].map((x) => Item.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "items": items == null
            ? null
            : List<dynamic>.from(items.map((x) => x.toJson())),
      };
}

class Item {
  Item({
    this.track,
  });

  final Track track;

  factory Item.fromJson(Map<String, dynamic> json) => Item(
        track: json["track"] == null ? null : Track.fromJson(json["track"]),
      );

  Map<String, dynamic> toJson() => {
        "track": track == null ? null : track.toJson(),
      };
}

class Track {
  Track({
    this.album,
    this.name,
    this.uri,
  });

  final Album album;
  final String name;
  final String uri;

  factory Track.fromJson(Map<String, dynamic> json) => Track(
        album: json["album"] == null ? null : Album.fromJson(json["album"]),
        name: json["name"] == null ? null : json["name"],
        uri: json["uri"] == null ? null : json["uri"],
      );

  Map<String, dynamic> toJson() => {
        "album": album == null ? null : album.toJson(),
        "name": name == null ? null : name,
        "uri": uri == null ? null : uri,
      };
}

class Album {
  Album({
    this.artists,
    this.images,
  });

  final List<Artist> artists;
  final List<Image> images;

  factory Album.fromJson(Map<String, dynamic> json) => Album(
        artists: json["artists"] == null
            ? null
            : List<Artist>.from(json["artists"].map((x) => Artist.fromJson(x))),
        images: json["images"] == null
            ? null
            : List<Image>.from(json["images"].map((x) => Image.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "artists": artists == null
            ? null
            : List<dynamic>.from(artists.map((x) => x.toJson())),
        "images": images == null
            ? null
            : List<dynamic>.from(images.map((x) => x.toJson())),
      };
}

class Artist {
  Artist({
    this.externalUrls,
    this.href,
    this.id,
    this.name,
    this.type,
    this.uri,
  });

  final ExternalUrls externalUrls;
  final String href;
  final String id;
  final String name;
  final String type;
  final String uri;

  factory Artist.fromJson(Map<String, dynamic> json) => Artist(
        externalUrls: json["external_urls"] == null
            ? null
            : ExternalUrls.fromJson(json["external_urls"]),
        href: json["href"] == null ? null : json["href"],
        id: json["id"] == null ? null : json["id"],
        name: json["name"] == null ? null : json["name"],
        type: json["type"] == null ? null : json["type"],
        uri: json["uri"] == null ? null : json["uri"],
      );

  Map<String, dynamic> toJson() => {
        "external_urls": externalUrls == null ? null : externalUrls.toJson(),
        "href": href == null ? null : href,
        "id": id == null ? null : id,
        "name": name == null ? null : name,
        "type": type == null ? null : type,
        "uri": uri == null ? null : uri,
      };
}

class ExternalUrls {
  ExternalUrls({
    this.spotify,
  });

  final String spotify;

  factory ExternalUrls.fromJson(Map<String, dynamic> json) => ExternalUrls(
        spotify: json["spotify"] == null ? null : json["spotify"],
      );

  Map<String, dynamic> toJson() => {
        "spotify": spotify == null ? null : spotify,
      };
}

class Image {
  Image({
    this.height,
    this.url,
    this.width,
  });

  final int height;
  final String url;
  final int width;

  factory Image.fromJson(Map<String, dynamic> json) => Image(
        height: json["height"] == null ? null : json["height"],
        url: json["url"] == null ? null : json["url"],
        width: json["width"] == null ? null : json["width"],
      );

  Map<String, dynamic> toJson() => {
        "height": height == null ? null : height,
        "url": url == null ? null : url,
        "width": width == null ? null : width,
      };
}

我还添加了json数据来查看错误所在:

{
  "tracks": {
    "items": [
      {
        "track": {
          "album": {
            "artists": [
              {
                "external_urls": {
                  "spotify": "https://open.spotify.com/artist/3vJJGsSAF5zQegZo5sJEh6"
                },
                "href": "https://api.spotify.com/v1/artists/3vJJGsSAF5zQegZo5sJEh6",
                "id": "3vJJGsSAF5zQegZo5sJEh6",
                "name": "Can Bonomo",
                "type": "artist",
                "uri": "spotify:artist:3vJJGsSAF5zQegZo5sJEh6"
              }
            ],
            "images": [
              {
                "height": 640,
                "url": "https://i.scdn.co/image/ab67616d0000b273438198f2165a7954bcfd9b81",
                "width": 640
              },
              {
                "height": 300,
                "url": "https://i.scdn.co/image/ab67616d00001e02438198f2165a7954bcfd9b81",
                "width": 300
              },
              {
                "height": 64,
                "url": "https://i.scdn.co/image/ab67616d00004851438198f2165a7954bcfd9b81",
                "width": 64
              }
            ]
          },
          "name": "Güneş",
          "uri": "spotify:track:4LmA7eKmD04MBZ4kHWXbLz"
        }
      },
      {
        "track": {
          "album": {
            "artists": [
              {
                "external_urls": {
                  "spotify": "https://open.spotify.com/artist/3vJJGsSAF5zQegZo5sJEh6"
                },
                "href": "https://api.spotify.com/v1/artists/3vJJGsSAF5zQegZo5sJEh6",
                "id": "3vJJGsSAF5zQegZo5sJEh6",
                "name": "Can Bonomo",
                "type": "artist",
                "uri": "spotify:artist:3vJJGsSAF5zQegZo5sJEh6"
              }
            ],
            "images": [
              {
                "height": 640,
                "url": "https://i.scdn.co/image/ab67616d0000b27380cb1ec0ca1c857dfccecfde",
                "width": 640
              },
              {
                "height": 300,
                "url": "https://i.scdn.co/image/ab67616d00001e0280cb1ec0ca1c857dfccecfde",
                "width": 300
              },
              {
                "height": 64,
                "url": "https://i.scdn.co/image/ab67616d0000485180cb1ec0ca1c857dfccecfde",
                "width": 64
              }
            ]
          },
          "name": "Dem",
          "uri": "spotify:track:2aUycmP66tY4wr3zt1sGc0"
        }
      },
      {
        "track": {
          "album": {
            "artists": [
              {
                "external_urls": {
                  "spotify": "https://open.spotify.com/artist/3vJJGsSAF5zQegZo5sJEh6"
                },
                "href": "https://api.spotify.com/v1/artists/3vJJGsSAF5zQegZo5sJEh6",
                "id": "3vJJGsSAF5zQegZo5sJEh6",
                "name": "Can Bonomo",
                "type": "artist",
                "uri": "spotify:artist:3vJJGsSAF5zQegZo5sJEh6"
              }
            ],
            "images": [
              {
                "height": 640,
                "url": "https://i.scdn.co/image/ab67616d0000b27380cb1ec0ca1c857dfccecfde",
                "width": 640
              },
              {
                "height": 300,
                "url": "https://i.scdn.co/image/ab67616d00001e0280cb1ec0ca1c857dfccecfde",
                "width": 300
              },
              {
                "height": 64,
                "url": "https://i.scdn.co/image/ab67616d0000485180cb1ec0ca1c857dfccecfde",
                "width": 64
              }
            ]
          },
          "name": "Tastamam",
          "uri": "spotify:track:3L8SvgfqK9Xb26rV4kq5Xt"
        }
      },
      {
        "track": {
          "album": {
            "artists": [
              {
                "external_urls": {
                  "spotify": "https://open.spotify.com/artist/3vJJGsSAF5zQegZo5sJEh6"
                },
                "href": "https://api.spotify.com/v1/artists/3vJJGsSAF5zQegZo5sJEh6",
                "id": "3vJJGsSAF5zQegZo5sJEh6",
                "name": "Can Bonomo",
                "type": "artist",
                "uri": "spotify:artist:3vJJGsSAF5zQegZo5sJEh6"
              }
            ],
            "images": [
              {
                "height": 640,
                "url": "https://i.scdn.co/image/ab67616d0000b27380cb1ec0ca1c857dfccecfde",
                "width": 640
              },
              {
                "height": 300,
                "url": "https://i.scdn.co/image/ab67616d00001e0280cb1ec0ca1c857dfccecfde",
                "width": 300
              },
              {
                "height": 64,
                "url": "https://i.scdn.co/image/ab67616d0000485180cb1ec0ca1c857dfccecfde",
                "width": 64
              }
            ]
          },
          "name": "Hikayem Bitmedi",
          "uri": "spotify:track:5YUjVwnxQeYJAxq6bElGWU"
        }
      },
      {
        "track": {
          "album": {
            "artists": [
              {
                "external_urls": {
                  "spotify": "https://open.spotify.com/artist/3vJJGsSAF5zQegZo5sJEh6"
                },
                "href": "https://api.spotify.com/v1/artists/3vJJGsSAF5zQegZo5sJEh6",
                "id": "3vJJGsSAF5zQegZo5sJEh6",
                "name": "Can Bonomo",
                "type": "artist",
                "uri": "spotify:artist:3vJJGsSAF5zQegZo5sJEh6"
              }
            ],
            "images": [
              {
                "height": 640,
                "url": "https://i.scdn.co/image/ab67616d0000b27380cb1ec0ca1c857dfccecfde",
                "width": 640
              },
              {
                "height": 300,
                "url": "https://i.scdn.co/image/ab67616d00001e0280cb1ec0ca1c857dfccecfde",
                "width": 300
              },
              {
                "height": 64,
                "url": "https://i.scdn.co/image/ab67616d0000485180cb1ec0ca1c857dfccecfde",
                "width": 64
              }
            ]
          },
          "name": "Her Kız Başka",
          "uri": "spotify:track:1kbHEESbmALWauoUtYPNAr"
        }
      },
      {
        "track": {
          "album": {
            "artists": [
              {
                "external_urls": {
                  "spotify": "https://open.spotify.com/artist/3vJJGsSAF5zQegZo5sJEh6"
                },
                "href": "https://api.spotify.com/v1/artists/3vJJGsSAF5zQegZo5sJEh6",
                "id": "3vJJGsSAF5zQegZo5sJEh6",
                "name": "Can Bonomo",
                "type": "artist",
                "uri": "spotify:artist:3vJJGsSAF5zQegZo5sJEh6"
              }
            ],
            "images": [
              {
                "height": 640,
                "url": "https://i.scdn.co/image/ab67616d0000b27380cb1ec0ca1c857dfccecfde",
                "width": 640
              },
              {
                "height": 300,
                "url": "https://i.scdn.co/image/ab67616d00001e0280cb1ec0ca1c857dfccecfde",
                "width": 300
              },
              {
                "height": 64,
                "url": "https://i.scdn.co/image/ab67616d0000485180cb1ec0ca1c857dfccecfde",
                "width": 64
              }
            ]
          },
          "name": "Senin Olmadan Ölemem",
          "uri": "spotify:track:5z1bH63TwXLslyDcDZKmqR"
        }
      },
      {
        "track": {
          "album": {
            "artists": [
              {
                "external_urls": {
                  "spotify": "https://open.spotify.com/artist/3vJJGsSAF5zQegZo5sJEh6"
                },
                "href": "https://api.spotify.com/v1/artists/3vJJGsSAF5zQegZo5sJEh6",
                "id": "3vJJGsSAF5zQegZo5sJEh6",
                "name": "Can Bonomo",
                "type": "artist",
                "uri": "spotify:artist:3vJJGsSAF5zQegZo5sJEh6"
              }
            ],
            "images": [
              {
                "height": 640,
                "url": "https://i.scdn.co/image/ab67616d0000b27380cb1ec0ca1c857dfccecfde",
                "width": 640
              },
              {
                "height": 300,
                "url": "https://i.scdn.co/image/ab67616d00001e0280cb1ec0ca1c857dfccecfde",
                "width": 300
              },
              {
                "height": 64,
                "url": "https://i.scdn.co/image/ab67616d0000485180cb1ec0ca1c857dfccecfde",
                "width": 64
              }
            ]
          },
          "name": "Bir Çocuk Yaralı",
          "uri": "spotify:track:6Czfr9RR7OpklOBHHcu658"
        }
      },
      {
        "track": {
          "album": {
            "artists": [
              {
                "external_urls": {
                  "spotify": "https://open.spotify.com/artist/3vJJGsSAF5zQegZo5sJEh6"
                },
                "href": "https://api.spotify.com/v1/artists/3vJJGsSAF5zQegZo5sJEh6",
                "id": "3vJJGsSAF5zQegZo5sJEh6",
                "name": "Can Bonomo",
                "type": "artist",
                "uri": "spotify:artist:3vJJGsSAF5zQegZo5sJEh6"
              }
            ],
            "images": [
              {
                "height": 640,
                "url": "https://i.scdn.co/image/ab67616d0000b27380cb1ec0ca1c857dfccecfde",
                "width": 640
              },
              {
                "height": 300,
                "url": "https://i.scdn.co/image/ab67616d00001e0280cb1ec0ca1c857dfccecfde",
                "width": 300
              },
              {
                "height": 64,
                "url": "https://i.scdn.co/image/ab67616d0000485180cb1ec0ca1c857dfccecfde",
                "width": 64
              }
            ]
          },
          "name": "Kaçak",
          "uri": "spotify:track:2R7w5iT5H43KmANEryoAn8"
        }
      },
      {
        "track": {
          "album": {
            "artists": [
              {
                "external_urls": {
                  "spotify": "https://open.spotify.com/artist/3vJJGsSAF5zQegZo5sJEh6"
                },
                "href": "https://api.spotify.com/v1/artists/3vJJGsSAF5zQegZo5sJEh6",
                "id": "3vJJGsSAF5zQegZo5sJEh6",
                "name": "Can Bonomo",
                "type": "artist",
                "uri": "spotify:artist:3vJJGsSAF5zQegZo5sJEh6"
              }
            ],
            "images": [
              {
                "height": 640,
                "url": "https://i.scdn.co/image/ab67616d0000b27380cb1ec0ca1c857dfccecfde",
                "width": 640
              },
              {
                "height": 300,
                "url": "https://i.scdn.co/image/ab67616d00001e0280cb1ec0ca1c857dfccecfde",
                "width": 300
              },
              {
                "height": 64,
                "url": "https://i.scdn.co/image/ab67616d0000485180cb1ec0ca1c857dfccecfde",
                "width": 64
              }
            ]
          },
          "name": "Resmini Görünce",
          "uri": "spotify:track:3lBfCNZ72P6ruxCkqV9lEu"
        }
      },
      {
        "track": {
          "album": {
            "artists": [
              {
                "external_urls": {
                  "spotify": "https://open.spotify.com/artist/3vJJGsSAF5zQegZo5sJEh6"
                },
                "href": "https://api.spotify.com/v1/artists/3vJJGsSAF5zQegZo5sJEh6",
                "id": "3vJJGsSAF5zQegZo5sJEh6",
                "name": "Can Bonomo",
                "type": "artist",
                "uri": "spotify:artist:3vJJGsSAF5zQegZo5sJEh6"
              }
            ],
            "images": [
              {
                "height": 640,
                "url": "https://i.scdn.co/image/ab67616d0000b27380cb1ec0ca1c857dfccecfde",
                "width": 640
              },
              {
                "height": 300,
                "url": "https://i.scdn.co/image/ab67616d00001e0280cb1ec0ca1c857dfccecfde",
                "width": 300
              },
              {
                "height": 64,
                "url": "https://i.scdn.co/image/ab67616d0000485180cb1ec0ca1c857dfccecfde",
                "width": 64
              }
            ]
          },
          "name": "Bulunmam Gerek",
          "uri": "spotify:track:6lTu9NoZWUqgh7ZxiAkU1O"
        }
      },
      {
        "track": {
          "album": {
            "artists": [
              {
                "external_urls": {
                  "spotify": "https://open.spotify.com/artist/3vJJGsSAF5zQegZo5sJEh6"
                },
                "href": "https://api.spotify.com/v1/artists/3vJJGsSAF5zQegZo5sJEh6",
                "id": "3vJJGsSAF5zQegZo5sJEh6",
                "name": "Can Bonomo",
                "type": "artist",
                "uri": "spotify:artist:3vJJGsSAF5zQegZo5sJEh6"
              }
            ],
            "images": [
              {
                "height": 640,
                "url": "https://i.scdn.co/image/ab67616d0000b27380cb1ec0ca1c857dfccecfde",
                "width": 640
              },
              {
                "height": 300,
                "url": "https://i.scdn.co/image/ab67616d00001e0280cb1ec0ca1c857dfccecfde",
                "width": 300
              },
              {
                "height": 64,
                "url": "https://i.scdn.co/image/ab67616d0000485180cb1ec0ca1c857dfccecfde",
                "width": 64
              }
            ]
          },
          "name": "Bahr-i Hazer",
          "uri": "spotify:track:0vxMon7God4sDfGZS4ImmC"
        }
      }
    ]
  }
}

最后是我的 api 请求。 我检查了答案是否正确。

static Future getTracks() async {
    var res = await http.get(
        "https://api.spotify.com/v1/playlists/6f3gB3WYLwTzAzUX3PdOmr?fields=tracks.items(track(name%2Curi%2Calbum(images%2Cartists)))",
        headers: headers);
    if (res.statusCode == 200) {
      return res;
    } else {
      throw Exception(res.statusCode);
    }
  }

最佳答案

var list 不是一个 List,就像您的代码所表明的那样。

该错误和您的示例 JSON 表明它是一个 MapMap 类的 map 方法采用与 Listmap 方法不同的函数参数。

您的 JSON 模型已创建为直接处理解析的 JSON,而无需在将其传递给 fromJson 构造函数之前进行任何映射。

只需删除 map 函数调用即可:

void getTracksFromApi() {
    PlayListApi.getTracks().then((response) {
      setState(() {
        var map = json.decode(response.body);
        Welcome modelObject = Welcome.fromJson(map);
        //this.trackList = ;This has to be modified to suit your particular needs
      });
    });
  }

关于json - 未处理的异常 : type '(dynamic) => Welcome' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63331381/

相关文章:

python - 使用 Python 和 Google API 进行反向地理编码

json - 如何通过 Dart 使用方法和数据发布到外部 API

logging - 创建一个中间件 Controller 以处理Dart中对Aqueduct的所有请求

flutter - FlutterWebviewPlugin监听器无法识别* .pdf为网址

json - 如何在 Postgres 中使用条件 WHERE json 层次结构列?

python - 如何配置Elasticsearch从Amazon SQS提取消息

json - PostgreSQL:比较 json

json - 使用 jq 展平/规范化 json 对象数组

azure - WebJobs 未通过在 powershell 脚本中使用 Rest API Post 方法来停止 - 出现错误 403 - 此 Web 应用程序已停止

c# - 带有枚举角色参数的自定义 AuthorizeAttributte 在 ajax 调用中获取空值