首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >未处理的异常:类型'(dynamic) => Welcome‘不是'transform’的类型'(String,dynamic) => MapEntry<dynamic,dynamic>‘的子类型

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

Stack Overflow用户
提问于 2020-08-10 05:12:17
回答 1查看 950关注 0票数 2

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

但我没想到这个错误是无法克服的。

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

我会展示我的代码和帮助,有人可以给我答案。

这是Android Studio的错误代码源。我在这里得到了错误:

代码语言:javascript
复制
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/模型:

代码语言:javascript
复制
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数据,以查看错误所在:

代码语言:javascript
复制
{
  "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请求。我检查了一下答案是否正确。

代码语言:javascript
复制
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);
    }
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-10 10:43:55

var list并不像您的代码所显示的那样是一个List

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

已经创建了JSON模型,以便直接处理解析后的JSON,而无需进行任何映射,然后再将其传递给fromJson构造函数。

只需删除map函数调用:

代码语言:javascript
复制
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
      });
    });
  }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63331381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档