发送端将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>'
}
发送端将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发送端代码:
副屏代码: