Skip to content

副屏接收数据转model类型转换问题 #13

@rongxianyu

Description

@rongxianyu

发送端将model转换成Map后发送到副屏,副屏再将接收到的JSON转换成model时,报如下错误,导致类型无法转换
Unhandled Exception: type '_Map<Object?, Object?>' is not a subtype of type 'Map<String, dynamic>'
JSON转model,model转JSON用三方库json_serializable

发送端代码:

@JsonSerializable()
class SubScreenMessage {
  int? code;

  dynamic message;

  SubScreenMessage();

  factory SubScreenMessage.fromJson(Map<String, dynamic> json) => _$SubScreenMessageFromJson(json);

  Map<String, dynamic> toJson() => _$SubScreenMessageToJson(this);
}
var mapObj = message.toJson();
print('发送消息到副屏消息 : $mapObj');
SubScreenPlugin.sendMsgToViceScreen('key',  params: mapObj);

副屏代码:

SubScreenPlugin.viceStream.listen((event) {
    // 接收到消息
    var message = SubScreenMessage.fromJson(event.arguments);
    // [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type '_Map<Object?, Object?>' is not a subtype of type 'Map<String, dynamic>'
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions