类型'_InternalLinkedHashMap <String,dynamic>'不是类型转换中'Session'类型的子类型

时间:2020-05-13 06:21:12

标签: android oop flutter dart mobile

Dart / Flutter的新功能。我创建了一个gridview,然后点击网格中的一个项目,我想将JSON数据传递到下一页。但是,我收到一个错误,我认为这与无法正确进行类型转换有关。我希望下一页显示标题,描述和图像(在gridview中显示的数据相同),但是出现一个错误,提示userId

这是我的代码:

type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Session' in type cast

我有点困,不胜感激!

1 个答案:

答案 0 :(得分:1)

因为您直接使用提取的json。首先,将您的body转换为Map

Map.from(jsonDecode(body));

正文就是您所用的_InternalLinkedHashMap<String, dynamic>数据。

然后将您的fromJson工厂添加到您的班级中:

class Session{
...
  factory Session.fromJson(Map<String, Object> json) {
    return Session(json['title'], json['description'], json['image']);
  }
...

然后根据您的请求使用解码后的Map,将其放在工厂中,即可获得对象。