json - 类型 'List' 不是类型 'List' 的子类型,其中

我是 Flutter 的新手,我尝试运行一个 GitHub 项目但收到如下错误:

type List dynamic is not a subtype of type List int where.

Github Link

错误线

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

}

Above code File

最佳答案

改变

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/

相关文章:

dart - 使用BottomNavigationBar导航时如何在 flutter 中保留小部件状

dart - 如何确定 Flutter 中图像的宽度和高度?

flutter - Flutter 框架中使用的键是什么?

flutter - Flutter 支持负边距吗?

asynchronous - 异步对调用者滚雪球,不能使构造函数异步

dart - Flutter 中具有 Snap 效果的水平滚动卡片

dart - 在 Flutter 中的小部件之间传递数据的最佳方式

Flutter - TabBarView 内部的 ListView 失去滚动位置

dart - Flutter 中的 Openstreetmap?

flutter - TextFormField 和 TextField 有什么区别?