gpt4 book ai didi

json - 未处理的异常:类型 '_InternalLinkedHashMap'不是类型转换中类型 'List'的子类型吗?

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

这是回应

{
"overview": "A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground "fight clubs" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.",
"title": "Fight Club",
}
该模型
class Movies {
String title;
String overview;


Movies(
{this.title , this.overview});

factory Movies.fromJson(Map <String, dynamic> parsedJson){
return Movies(title: parsedJson['title'] , overview: parsedJson['overview']);
}

}
  Future <List<Movies>> fetchMovies () async {
var response = await http.get(url);
var jsonData = jsonDecode(response.body) as List ;
List<Movies> movies = jsonData.map((e) => Movies.fromJson(e)).toList();

print(movies.length);
return movies;

}
我收到此错误(未处理的异常:类型'_InternalLinkedHashMap '不是强制类型转换中类型'List'的子类型)

最佳答案

您尝试将jsonData转换为List,但解码器说它的类型为Map(也显示为Map类型)

Future <List<Movies>> fetchMovies () async {
var response = await http.get(url);
var jsonData = jsonDecode(response.body);
if(jsonData is List) //check if it's a List
return List<Movies>.from(jsonData.map(map) => Movies.fromJson(map));
else if(jsonData is Map) //check if it's a Map
return [Movies.fromJson(jsonData)]; //return a List of length 1

}

关于json - 未处理的异常:类型 '_InternalLinkedHashMap<String, dynamic>'不是类型转换中类型 'List<dynamic>'的子类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62650237/

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