输入“列表'不是'List类型的子类型'在哪里
问题内容:
我是Flutter的新手,我尝试运行一个github项目,但遇到类似类型List dynamic的错误,而不是类型List
int的子类型。Github链接
错误线
List<int> genreIds;
MediaItem._internalFromJson(Map jsonMap, {MediaType type: MediaType.movie})
:
type = type,
id = jsonMap["id"].toInt(),
voteAverage = jsonMap["vote_average"].toDouble(),
title = jsonMap[(type == MediaType.movie ? "title" : "name")],
posterPath = jsonMap["poster_path"] ?? "",
backdropPath = jsonMap["backdrop_path"] ?? "",
overview = jsonMap["overview"],
releaseDate = jsonMap[(type == MediaType.movie
? "release_date"
: "first_air_date")],
genreIds = jsonMap["genre_ids"];//in this line
}
任何帮助将不胜感激,在此先感谢您。
问题答案:
更改
genreIds = jsonMap["genre_ids"];
至
genreIds = jsonMap["genre_ids"].cast<int>();
JSON映射或列表中的类型没有具体的泛型类型。
genreIds
要求List<int>
不是List
(或List<dynamic>
),因此您需要将值带入所需的类型,然后才能进行分配。
如果您之前没有在相同的代码中看到此错误,则可能是因为您升级到了Dart版本,--preview-dart-2
而该版本已成为默认版本(之前已选择启用)