简体   繁体   English

Flutter dart json 未处理的异常:InternalLinkedHashMap<string, dynamic> ' 不是类型 'List 的子类型<dynamic></dynamic></string,>

[英]Flutter dart json Unhandled Exception: InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>

I have this code from my other question (thanks to chunhunghan).我从另一个问题中得到了这段代码(感谢 chunhunghan)。 I need to make a log in screen and im trying to validate user credentials against a response the server sends back to me but every time i try to run the code it gives me the "Unhandled Exception: InternalLinkedHashMap' is not a subtype of type 'List" error and im not sure how to fix it.我需要创建一个登录屏幕,并尝试根据服务器发回给我的响应来验证用户凭据,但是每次我尝试运行代码时,它都会给我“未处理的异常:InternalLinkedHashMap”不是类型的子类型列表”错误,我不确定如何修复它。

thats the model:这就是 model:

import 'dart:convert';
Payload payloadFromJson(String str) => Payload.fromJson(json.decode(str));

String payloadToJson(Payload data) => json.encode(data.toJson());

class Payload {
  Payload({
    this.version,
    this.encoding,
    this.subsonicResponse,
  });

  String version;
  String encoding;
  SubsonicResponse subsonicResponse;

  factory Payload.fromJson(Map<String, dynamic> json) => Payload(
        version: json["version"],
        encoding: json["encoding"],
        subsonicResponse: SubsonicResponse.fromJson(json["subsonic-response"]),
      );

  Map<String, dynamic> toJson() => {
        "version": version,
        "encoding": encoding,
        "subsonic-response": subsonicResponse.toJson(),
      };
}

class SubsonicResponse {
  SubsonicResponse({
    this.status,
    this.version,
    this.xmlns,
    this.albumList,
  });

  String status;
  String version;
  String xmlns;
  AlbumList albumList;

  factory SubsonicResponse.fromJson(Map<String, dynamic> json) =>
      SubsonicResponse(
        status: json["status"],
        version: json["version"],
        albumList: AlbumList.fromJson(json["albumList"]),
      );

  Map<String, dynamic> toJson() => {
        "status": status,
        "version": version,
        "albumList": albumList.toJson(),
      };
}

class AlbumList {
  AlbumList({
    this.album,
  });

  List<AlbumsList> album;

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

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

class AlbumsList {
  AlbumsList({
    this.id,
    this.parent,
    this.isDir,
    this.title,
    this.album,
    this.artist,
    this.genre,
    this.coverArt,
    this.playCount,
    this.created,
    this.year,
  });

  String id;
  String parent;
  String isDir;
  String title;
  String album;
  String artist;
  String genre;
  String coverArt;
  String playCount;
  DateTime created;
  String year;

  factory AlbumsList.fromJson(Map<String, dynamic> json) => AlbumsList(
        id: json["id"],
        parent: json["parent"],
        isDir: json["isDir"],
        title: json["title"],
        album: json["album"],
        artist: json["artist"],
        genre: json["genre"],
        coverArt: json["coverArt"],
        playCount: json["playCount"],
        created: DateTime.parse(json["created"]),
        year: json["year"] == null ? null : json["year"],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "parent": parent,
        "isDir": isDir,
        "title": title,
        "album": album,
        "artist": artist,
        "genre": genre,
        "coverArt": coverArt,
        "playCount": playCount,
        "created": created.toIso8601String(),
        "year": year == null ? null : year,
      };
}

here is the request:这是请求:

Future <Payload>fetchResponse() async{
final authresponse = await http.get(authURL);
if (authresponse.statusCode == 200){
final jsondata = jsonDecode(authresponse.body);
return payloadFromJson(jsondata);
}else
throw Exception("Unable to connect to server, try again");
}

it gives me the error on the return line.它给了我返回线上的错误。 EDIT: i forgot to add the api response.编辑:我忘了添加 api 响应。

{
   "subsonic-response": {
      "status": "ok",
      "version": "1.15.0"
   }
}

this is the api response这是 api 响应

change payloadFromJson topayloadFromJson更改为

Payload payloadFromJson (Map<String, dynamic>str) => Payload.fromJson(str);

fetch value from json if the json contains the key如果 json 包含密钥,则从 json 获取值

 factory SubsonicResponse.fromJson(Map<String, dynamic> json) =>
      SubsonicResponse(
        status: json["status"],
        version: json["version"],
        albumList: json.containsKey("albumList")? AlbumList.fromJson(json["albumList"]):null,
      );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dio.get - 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是 Flutter/Dart 中 'String' 类型的子类型</string,> - Dio.get - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' in Flutter/Dart 未处理的异常:InternalLinkedHashMap<string, dynamic> ' 不是类型 'List 的子类型<dynamic></dynamic></string,> - Unhandled Exception: InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic> 从 api 错误中获取 Json 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型 'List 的子类型<dynamic> '</dynamic></string,> - Fetching Json from api error Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>' 在 dart 中解析对象(未处理的异常:类型 &#39;_InternalLinkedHashMap<dynamic, dynamic> &#39; 不是类型 &#39;Map 的子类型<String, dynamic> &#39;) - Parsing object in dart (Unhandled Exception: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>') 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是 'HashMap 类型的子类型<string, dynamic> ' Dart</string,></string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'HashMap<String, dynamic>' Dart Dart 未处理的异常:类型 &#39;_InternalLinkedHashMap<String, dynamic> &#39; 不是 &#39;Iterable 类型的子类型<dynamic> - Dart Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic> E/Flutter 未处理异常:类型 _InternalLinkedHashMap<dynamic, dynamic> ' 不是类型 'Map 的子类型<string, string> ?</string,></dynamic,> - E/Flutter Unhandled Exception: type _InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, String>? Flutter 未处理的异常:类型 &#39;_InternalLinkedHashMap<String, dynamic> JSON 请求 - Flutter Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic> JSON request 未处理的异常:键入“列表”<dynamic> ' 不是类型 'Map 的子类型<string, dynamic> ' 在飞镖/ flutter</string,></dynamic> - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in dart/ flutter 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型 'String' 的子类型</string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM