相关文章推荐
儒雅的钢笔  ·  HttpClient的BaseAddress ...·  1 周前    · 
大力的熊猫  ·  HttpClient.GetAsync ...·  1 周前    · 
风度翩翩的钢笔  ·  encodeURI() - ...·  1 周前    · 
低调的海豚  ·  WPF ...·  1 年前    · 
博学的甘蔗  ·  linux screen 命令详解 - ...·  1 年前    · 
发呆的花生  ·  How does Java ...·  1 年前    · 
没读研的大葱  ·  Comparer<t> 类 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

I am trying data serialization from json to dart.

But I didn't this error can't get over.

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

I will show my codes and helpful someone can give answer me.

This code source of error to Android Studio. I'm getting the error here:

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');

Maybe the problem is in my models. I built with these two tools https://app.quicktype.io and https://javiercbk.github.io/json_to_dart/ Models in here:

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,

I am also adding the json data to see where the error is:

"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"

Finally, my api request. I checked if the answer was correct.

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 is not a List like your code indicates you think it is.

The error and your sample JSON indicate that it is a Map. Map class's map method takes a different function parameter than the map method of Lists.

Your JSON model was already created to handle the parsed JSON directly without doing any mapping before passing it to the fromJson constructor.

Just remove the map function call:

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
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.