简体   繁体   English

在 dart 中解析对象(未处理的异常:类型 &#39;_InternalLinkedHashMap<dynamic, dynamic> &#39; 不是类型 &#39;Map 的子类型<String, dynamic> &#39;)

[英]Parsing object in dart (Unhandled Exception: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>')

When I try to convert json to an object this error comes up:当我尝试将 json 转换为对象时,出现此错误:

Unhandled Exception: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'

It happens only when I try to convert List of objects type and the Chat object, here is the class and the function fromJson to an object.只有当我尝试将对象List类型和Chat对象转换时才会发生这种情况,这里是类和函数fromJson到一个对象。 I don't know what's wrong with the conversion.我不知道转换有什么问题。

class Project {
  String projectID;
  String projectName;
  String image;
  String joiningLink;
  bool isJoiningLinkEnabled;
  String pinnedMessage;
  Chat chat;
  List<Event> event;
  List<Member> members;
  List<String> membersIDs;
  List<Task> task;

  Project(
      {this.projectID,
        this.projectName,
        this.image,
        this.joiningLink,
        this.isJoiningLinkEnabled,
        this.pinnedMessage,
        this.chat,
        this.event,
        this.members,
        this.membersIDs,
        this.task});

  // Exception shows here
  Project.fromJson(Map<String, dynamic> json) {
    projectID = json['projectID'];
    projectName = json['projectName'];
    image = json['image'];
    joiningLink = json['joiningLink'];
    isJoiningLinkEnabled = json['isJoiningLinkEnabled'];
    pinnedMessage = json['pinnedMessage'];
    if (json['event'] != null) {
      event = new List<Event>();
      json['event'].forEach((v) {
        event.add(new Event.fromJson(v));
      });
    }
    chat = json['chat'] != null ? new Chat.fromJson(json['chat']) : null;
    if (json['members'] != null) {
      members = new List<Member>();
      json['members'].forEach((v) {
        members.add(new Member.fromJson(v));
      });
    }
    membersIDs = List.of(json['membersIDs'].cast<String>());
    if (json['Task'] != null) {
      task = new List<Task>();
      json['Task'].forEach((v) {
        task.add(new Task.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['projectID'] = this.projectID;
    data['projectName'] = this.projectName;
    data['image'] = this.image;
    data['joiningLink'] = this.joiningLink;
    data['isJoiningLinkEnabled'] = this.isJoiningLinkEnabled;
    data['pinnedMessage'] = this.pinnedMessage;
    if (this.chat != null) {
      data['chat'] = this.chat.toJson();
    }
    if (this.event != null) {
      data['event'] = this.event.map((v) => v.toJson()).toList();
    }
    if (this.members != null) {
      data['members'] = this.members.map((v) => v.toJson()).toList();
    }
    data['membersIDs'] = this.membersIDs;
    if (this.task != null) {
      data['Task'] = this.task.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

似乎解码器( json.decoder )解码地图中的所有对象,但聊天的解码方式无法将键解释为String ,因此它使用<dynamic, dynamic> ,您可以尝试创建一个映射将动态转换为 String 并将其作为Chat.fromJson()的参数Chat.fromJson()我相信它接受 Map<String, dynamic> 作为参数,这就是它触发错误的原因)

chat = json['chat'] != null ? new Chat.fromJson(Map<String,dynamic>.from(json['chat'])) : null;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是 'HashMap 类型的子类型<string, dynamic> ' Dart</string,></string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'HashMap<String, dynamic>' Dart Dart 未处理的异常:类型 &#39;_InternalLinkedHashMap<String, dynamic> &#39; 不是 &#39;Iterable 类型的子类型<dynamic> - Dart Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic> 未处理的异常:InternalLinkedHashMap<string, dynamic> ' 不是类型 'List 的子类型<dynamic></dynamic></string,> - Unhandled Exception: InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic> E/Flutter 未处理异常:类型 _InternalLinkedHashMap<dynamic, dynamic> ' 不是类型 'Map 的子类型<string, string> ?</string,></dynamic,> - E/Flutter Unhandled Exception: type _InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, String>? Flutter dart json 未处理的异常:InternalLinkedHashMap<string, dynamic> ' 不是类型 'List 的子类型<dynamic></dynamic></string,> - Flutter dart json Unhandled Exception: InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic> 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型 'String' 的子类型</string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型 'Response' 的子类型</string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Response' Dio.get - 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是 Flutter/Dart 中 'String' 类型的子类型</string,> - Dio.get - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' in Flutter/Dart 嵌套对象 - 未处理的异常:类型 &#39;_InternalLinkedHashMap<String, dynamic> &#39; 不是 &#39;Iterable 类型的子类型<dynamic> - Nested Object - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic> 未处理的异常:类型 '_InternalLinkedHashMap<dynamic, dynamic> ' 不是类型 'Map 的子类型<datetime, list<account> &gt;' 类型转换</datetime,></dynamic,> - Unhandled Exception: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<DateTime, List<Account>>' in type cast
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM