用于扩展子级的 toJson 方法的父类 - 或者如何使用干净的架构模式嵌套 json

2024-01-04

我正在调整我的代码以适应干净的架构(遵循 Reso Coder 的精彩教程),并且现在在嵌套结构上的 toJson 方法上遇到了问题。与我之前的代码相比,不同之处在于数据模型类是从数据实体类扩展而来的。

我发现了完全相同的问题here https://stackoverflow.com/questions/65541985/clean-architecture-tojson-with-sublist-reso-coder但是,我想知道是否有更好/更清洁的解决方案?!

class MyData extends Equatable{
  final List<MyNestedData> nested; 
  MyData(this.nested);
}

class MyNestedData extends Equatable {
  final int x;
  final int y;
  MyNestedData({this.x, this.y});
}
class MyDataModel extends MyData{
  final List<MyNestedData> nested; 
  MyDataModel(List<MyNestedDataModel> nested) : super (nested);
  
  Map<String, dynamic> toJson() => {
    "nested": List<dynamic>.from(nested.map((x) => x.toJson())),
  };
}

class MyNestedDataModel extends MyNestedData {
  MyNestedData({x, y}) : super (x: x, y: y);
  
  Map<String, dynamic> toJson() => {
    x: x,
    y: y
  }
}

MyDataModel 显然忽略了调整后的参数定义List<MyNestedDataModel> nested和用途List<MyNestedData>而是父母的。那么我将如何处理 toJson 方法MyNestedDataModel反而?


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

用于扩展子级的 toJson 方法的父类 - 或者如何使用干净的架构模式嵌套 json 的相关文章

随机推荐