我是 Flutter 的新手,我尝试运行一个 GitHub 项目但收到如下错误:
type List dynamic is not a subtype of type List int where.
错误线
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>
),因此您需要先将值转换为所需的类型,然后才能分配它。
如果您之前没有看到相同代码的此错误,那可能是因为您升级到了 --preview-dart-2
的 Dart 版本。成为默认值(之前是选择加入的)
关于json - 类型 'List<dynamic>' 不是类型 'List<int>' 的子类型,其中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50245187/