Flutter 发生异常:类型';MappedListIterable<;动态、体裁>';不是类型为';列表<;流派>';stackTrace:#0新的GenreResponse.fromJson
我在颤振项目中遇到异常错误,有人能帮我吗Flutter 发生异常:类型';MappedListIterable<;动态、体裁>';不是类型为';列表<;流派>';stackTrace:#0新的GenreResponse.fromJson,flutter,dart,flutter-layout,Flutter,Dart,Flutter Layout,我在颤振项目中遇到异常错误,有人能帮我吗 class GenreResponse { final List<Genre> genres; final String error; GenreResponse(this.genres, this.error); GenreResponse.fromJson(Map<String, dynamic> json) : genres = (json["genres"] as Li
class GenreResponse {
final List<Genre> genres;
final String error;
GenreResponse(this.genres, this.error);
GenreResponse.fromJson(Map<String, dynamic> json)
: genres = (json["genres"] as List).map((e) => new Genre.fromJson(json)),
error = "";
GenreResponse.withError(String errorValue)
: genres = List(),
error = errorValue;
}
类GenreResponse{
最终列表类型;
最终字符串错误;
流派响应(this.genres,this.error);
GenreResponse.fromJson(映射json)
:genres=(json[“genres”]作为列表).map((e)=>new-Genre.fromJson(json)),
错误=”;
GenreResponse.withError(字符串错误值)
:genres=List(),
误差=误差值;
}
另外,为了更好地理解,我在这里添加了我的genre.dart代码
final int id;
final String name;
Genre(this.id, this.name);
Genre.fromJson(Map<String, dynamic> json)
: id = json["id"],
name = json["name"];
}
final int-id;
最后的字符串名;
类型(this.id,this.name);
fromJson(映射json)
:id=json[“id”],
name=json[“name”];
}
只需将.toList()
添加到.map()
行:
GenreResponse.fromJson(Map<String, dynamic> json)
: genres = (json["genres"] as List).map((e) => new Genre.fromJson(json)).toList(),
error = "";
GenreResponse.fromJson(映射json)
:genres=(json[“genres”]作为列表).map((e)=>new-Genre.fromJson(json)).toList(),
错误=”;
这是因为
.map()
的输出是MappedListIterable
而不是列表。令人恼火的是编译器没有捕捉到这一点,但我认为这与继承的工作方式有关。请分享打印JSON映射的输出。