带有继承类的 flutter Dart JsonSerializable

标签 flutter inheritance dart jsonserializer

我有以下两个类,其中一个类是从另一个类扩展而来的:

@JsonSerializable(nullable: true)
class Response {
  final String responseCode;
  final String responseMessage;
  final String errorLog;
  Response({this.errorLog, this.responseCode, this.responseMessage});
  factory Response.fromJson(Map<String, dynamic> json) =>
      _$ResponseFromJson(json);
}

………………………………………………………………………………………………………………………………………………………… ......
 @JsonSerializable(nullable: false)
class Verify extends Response {
  Data data;
  Verify({
    this.data,
  });
  factory Verify.fromJson(Map<String, dynamic> json) => _$VerifyFromJson(json);
  Map<String, dynamic> toJson() => _$VerifyToJson(this);
}

每当我尝试阅读时 回复 来自验证 clasit 的类属性始终为空

那么请如何实现这一目标?

最佳答案

这个我通过在验证类构造函数中将参数传递给 super 来解决的

@JsonSerializable()
class VerifyResponse extends Response {
  Data data;

  VerifyResponse({
    this.data,
    String responseCode,
    String responseMessage,
  }) : super(responseCode: responseCode, responseMessage: responseMessage);

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

  Map<String, dynamic> toJson() => _$VerifyResponseToJson(this);
}
对于响应类,它保持不变
@JsonSerializable()
class Response {
  final String responseCode;
  final String responseMessage;

  Response({this.responseCode, this.responseMessage});

  factory Response.fromJson(Map<String, dynamic> json) =>
      _$ResponseFromJson(json);
}
这有点烦人,但就是这样。

关于带有继承类的 flutter Dart JsonSerializable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62144987/

相关文章:

firebase - Flutter:没有为类 DataSnapshot 定义 forEach 方法

异常(exception):迭代期间的并发修改: '_GrowableList' 的实例(长度:17)

firebase - NoSuchMethodError:在null上调用getter数据,在receiver上调用null

ios - Flutter 不会构建 iOS 版本 - 完成

xcode - 在 Runner.xcodeproj/project.pbxproj 中硬编码 FLUTTER_ROOT

flutter - 使用flutter Provider小部件时出现以下错误

php - 学说 2.2 ORM : find extended entity

C++基本多态性

python - pd.pivot_table 如何/在哪里存储或引用其索引和列变量的名称?

html - 从自定义包中加载聚合物元素