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

Unhandled Exception: type '(dynamic) => Welcome' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform'

我正在尝试从 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.io and https://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 class 的 map 方法采用与 Lists 的 map 方法不同的函数参数。

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

只需删除 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
      });
    });
  }