我是Flutter的新手,我尝试运行一个github项目,但得到类似List动态的错误不是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
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,谢谢你提前.
在飞镖,有什么之间的区别List.from和List.of之间,以及Map.from和Map.of?他们的文档并不十分清楚:
/**
* Creates a [LinkedHashMap] instance that contains all key/value pairs of
* [other].
*
* The keys must all be instances of [K] and the values of [V].
* The [other] map itself can have any type.
*
* A `LinkedHashMap` requires the keys to implement compatible
* `operator==` and `hashCode`, and it allows `null` as a key.
* It iterates in key insertion order.
*/
factory Map.from(Map other) = …Run Code Online (Sandbox Code Playgroud) 即将发布的 Dart 2.X 版本需要强类型。在处理 JSON 数据时,我们现在必须将dynamic类型转换为适当的 Dart 类型(不是问题)。一个相关的问题Ignoring cast fail from JSArray to List<String>提供了使用该.cast<String>()函数的答案。最近的一组消息也有同样的说法:
Breaking Change: --preview-dart-2 enabled by default。
问题是该.cast()函数似乎没有组合。使用 DDC 编译并在 Chrome 浏览器中运行时的原始代码:
Map<String, dynamic> json = { "data": ["a", "b", "c"] };
List<String> origBroken = json["data"].map( (s) => s.toUpperCase() ).toList();
Run Code Online (Sandbox Code Playgroud)
现在收到运行时警告(很快就会出错)
Ignoring cast fail from JSArray to List<String>
Run Code Online (Sandbox Code Playgroud)
所以我.cast<String>()按照文档和相关链接的建议添加,但仍然收到警告:
List<String> docFixBroken = json["data"].cast<String>().map( (s) => s.toUpperCase() ).toList();
List<String> alsoBroken = List.from( (json["data"] as List).cast<String>() ).map( (s) …Run Code Online (Sandbox Code Playgroud)