gpt4 book ai didi

json - Flutter dart json未处理的异常: InternalLinkedHashMap' is not a subtype of type '列表<动态>

转载 作者:行者123 更新时间:2023-12-03 04:46:27 27 4
gpt4 key购买 nike

我从另一个问题中得到了这段代码(感谢 chunhunghan)。我需要创建一个登录屏幕,并尝试根据服务器发回给我的响应来验证用户凭据,但是每次我尝试运行代码时,它都会给我“未处理的异常:InternalLinkedHashMap”不是类型的子类型列表”错误,我不确定如何修复它。

这就是模型:

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,
};
}

这是请求:
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");
}

它给了我返回线上的错误。
编辑:我忘了添加 api 响应。
{
"subsonic-response": {
"status": "ok",
"version": "1.15.0"
}
}

这是 api 响应

最佳答案

更改 payloadFromJson

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

如果 json 包含 key ,则从 json 获取值
 factory SubsonicResponse.fromJson(Map<String, dynamic> json) =>
SubsonicResponse(
status: json["status"],
version: json["version"],
albumList: json.containsKey("albumList")? AlbumList.fromJson(json["albumList"]):null,
);

关于json - Flutter dart json未处理的异常: InternalLinkedHashMap<String,动态>' is not a subtype of type '列表<动态>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62186449/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com