【发布时间】:2021-06-14 05:10:27
【问题描述】:
我想根据类型将嵌套模型转换为正确的模型。我知道@JsonKey 可以用来专门处理某些属性。但是,如何访问 fromJson 上的其他属性? fromJson 方法必须是静态的,但是我无法访问其他属性。有谁知道如何解决这个问题?
@JsonSerializable(explicitToJson: true, nullable: true)
class Model {
int type;
@JsonKey(
name: 'action',
fromJson: _convertActionToModel,
)
dynamic action;
Model({this.type, this.action});
factory Model.fromJson(Map<String, dynamic> json) =>
_$ModelFromJson(json);
Map<String, dynamic> toJson() => _$ModelToJson(this);
static dynamic _convertActionToModel(dynamic json) {
switch (type) { // How can i get this type?
case 0:
return OtherModel.fromJson(json as Map<String, dynamic>);
break;
....
}
}
如何获取开关盒的类型?
【问题讨论】:
标签: json flutter dart serialization