我目前正在学习 Flutter 和 Dart 以尝试扩展我的知识。我遇到的问题是由于从 Dart 1.0 更改为 Dart 2.0,导致我认为问题在于类型模式的更改。我的代码:
我正在使用执行此操作的 Spotify 库:
object.artists = (map['artists'] as List<dynamic>)
?.map(ArtistSimpleMapper.parse)
?.toList();
ArtistsSimpleMapper 类的 parse 方法如下:
/// Converts a Map to an instance of Image.
static Image parse(Map<String, dynamic> map) {
if (map == null) return null;
final Image object = new Image();
object.height = map['height'];
object.width = map['width'];
object.url = map['url'];
return object;
}
我最成功的方法是使用.cast<dynamic>()
,代码在运行时而不是编译时失败,不用说我完全不知道如何遵循或如何解决这个问题。
错误:
Error: A value of type '(dart.core::Map<dart.core::String, dynamic>) → spotify::ArtistSimple' can't be assigned to a variable of type '(dynamic) → dart.core::Object'.
?.map(ArtistSimpleMapper.parse)
^
文章阅读:链接