扩展由自动生成的 Amplify Datastore 类与工厂构造函数

huangapple go评论64阅读模式
英文:

Extending auto-generated Amplify Datastore classes with factory constructors

问题

以下是要翻译的代码部分:

// 通过AWS Amplify Studio生成的基本模型:
@immutable
class Patient extends Model {
  static const classType = const _PatientModelType();
  final String id;
  final String? _name;
  final String? _family_name;
  final TemporalDate? _dob;
  final Gender? _gender;
  final double? _height_in_inches;
  final List<Assessment>? _Assessments;
  final TemporalDateTime? _createdAt;
  final TemporalDateTime? _updatedAt;

  // ...

  const Patient._internal({
    required this.id,
    required name,
    required family_name,
    required dob,
    required gender,
    required height_in_inches,
    Assessments,
    createdAt,
    updatedAt,
  })  : _name = name,
        _family_name = family_name,
        _dob = dob,
        _gender = gender,
        _height_in_inches = height_in_inches,
        _Assessments = Assessments,
        _createdAt = createdAt,
        _updatedAt = updatedAt;

  factory Patient({
    String? id,
    required String name,
    required String family_name,
    required TemporalDate dob,
    required Gender gender,
    required double height_in_inches,
    List<Assessment>? Assessments,
  }) {
    return Patient._internal(
      id: id == null ? UUID.getUUID() : id,
      name: name,
      family_name: family_name,
      dob: dob,
      gender: gender,
      height_in_inches: height_in_inches,
      Assessments: Assessments != null
          ? List<Assessment>.unmodifiable(Assessments)
          : Assessments,
    );
  }

  // ...

  Patient.fromJson(Map<String, dynamic> json)
      : id = json['id'],
        _name = json['name'],
        _family_name = json['family_name'],
        _dob = json['dob'] != null ? TemporalDate.fromString(json['dob']) : null,
        _gender = enumFromString<Gender>(json['gender'], Gender.values),
        _height_in_inches = (json['height_in_inches'] as num?)?.toDouble(),
        _Assessments = json['Assessments'] is List
            ? (json['Assessments'] as List)
                .where((e) => e?['serializedData'] != null)
                .map((e) => Assessment.fromJson(new Map<String, dynamic>.from(e['serializedData'])))
                .toList()
            : null,
        _createdAt = json['createdAt'] != null ? TemporalDateTime.fromString(json['createdAt']) : null,
        _updatedAt = json['updatedAt'] != null ? TemporalDateTime.fromString(json['updatedAt']) : null;

  // ...
}

你想创建一个扩展类GaitPatient。但是,你遇到了以下错误:

超类'Patient'的无名构造函数(被'GaitPatient'的默认构造函数调用)必须是一个生成构造函数,但找到的是工厂构造函数。尝试添加一个具有不同超类初始化程序的显式构造函数,或将超类构造函数'Patient Patient(...)'更改为非工厂构造函数

你目前正在考虑使用Patient.fromJSON构造函数,但对于使用它有些犹豫,因为它似乎不是最清晰的...在这里你有哪些选择?

如果你要创建GaitPatient的扩展类,你可以考虑以下选项:

  1. 显式构造函数: 你可以在GaitPatient类中创建一个显式构造函数,该构造函数使用super调用Patient.fromJson构造函数,以确保超类构造函数是生成构造函数而不是工厂构造函数。这可能是最直接的方法,可以避免使用工厂构造函数。
class GaitPatient extends Patient {
  GaitPatient.fromJson(Map<String, dynamic> json) : super.fromJson(json);
  // Add any additional properties or methods for GaitPatient.
}
  1. 使用工厂构造函数: 如果你决定继续使用工厂构造函数,可以尝试将Patient类的构造函数更改为生成构造函数,以便GaitPatient可以扩展它。这可能需要更多的修改,但可以让你继续使用GaitPatient的工厂构造函数。
class Patient extends Model {
  // 修改为生成构造函数
  Patient._internal({
    required this.id,
    required name,
    required family_name,
    required dob,
    required gender,
    required height_in_inches,
    Assessments,
    createdAt,
    updatedAt,
  }) : ...
}

然后,你可以创建GaitPatient类的工厂构造函数,类似于Patient

不管你选择哪种方法,确保适当处理超类的构造函数,以避免出现错误。

英文:

The base model generated through AWS Amplify Studio:

@immutable
class Patient extends Model {
  static const classType = const _PatientModelType();
  final String id;
  final String? _name;
  final String? _family_name;
  final TemporalDate? _dob;
  final Gender? _gender;
  final double? _height_in_inches;
  final List&lt;Assessment&gt;? _Assessments;
  final TemporalDateTime? _createdAt;
  final TemporalDateTime? _updatedAt;

 ...
  
  const Patient._internal({required this.id, required name, required family_name, required dob, required gender, required height_in_inches, Assessments, createdAt, updatedAt}): _name = name, _family_name = family_name, _dob = dob, _gender = gender, _height_in_inches = height_in_inches, _Assessments = Assessments, _createdAt = createdAt, _updatedAt = updatedAt;
  
  factory Patient({String? id, required String name, required String family_name, required TemporalDate dob, required Gender gender, required double height_in_inches, List&lt;Assessment&gt;? Assessments}) {
    return Patient._internal(
      id: id == null ? UUID.getUUID() : id,
      name: name,
      family_name: family_name,
      dob: dob,
      gender: gender,
      height_in_inches: height_in_inches,
      Assessments: Assessments != null ? List&lt;Assessment&gt;.unmodifiable(Assessments) : Assessments);
  }
  
  ...
  
  Patient.fromJson(Map&lt;String, dynamic&gt; json)  
    : id = json[&#39;id&#39;],
      _name = json[&#39;name&#39;],
      _family_name = json[&#39;family_name&#39;],
      _dob = json[&#39;dob&#39;] != null ? TemporalDate.fromString(json[&#39;dob&#39;]) : null,
      _gender = enumFromString&lt;Gender&gt;(json[&#39;gender&#39;], Gender.values),
      _height_in_inches = (json[&#39;height_in_inches&#39;] as num?)?.toDouble(),
      _Assessments = json[&#39;Assessments&#39;] is List
        ? (json[&#39;Assessments&#39;] as List)
          .where((e) =&gt; e?[&#39;serializedData&#39;] != null)
          .map((e) =&gt; Assessment.fromJson(new Map&lt;String, dynamic&gt;.from(e[&#39;serializedData&#39;])))
          .toList()
        : null,
      _createdAt = json[&#39;createdAt&#39;] != null ? TemporalDateTime.fromString(json[&#39;createdAt&#39;]) : null,
      _updatedAt = json[&#39;updatedAt&#39;] != null ? TemporalDateTime.fromString(json[&#39;updatedAt&#39;]) : null;
  
  ...
}

I want to create an extension class GaitPatient. But, I am running into this error,

>The unnamed constructor of superclass 'Patient' (called by the default constructor of 'GaitPatient') must be a generative constructor, but factory found. Try adding an explicit constructor that has a different superinitializer or changing the superclass constructor 'Patient Patient(...)' to not be a factory constructor

I am currently looking at using Patient.fromJSON constructor, but a bit hesitant about using that because it doesn't seem to be the cleanest... what are my options here?

答案1

得分: 1

以下是代码的翻译部分:

<!-- 开始代码段: JavaScript 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-html -->
@immutable
class GaitPatient extends Patient {
  final double? gaitSpeed;

  const GaitPatient._internal({
    required String id, 
    required String name, 
    required String family_name, 
    required TemporalDate dob, 
    required Gender gender, 
    required double height_in_inches, 
    List<Assessment>? Assessments, 
    TemporalDateTime? createdAt, 
    TemporalDateTime? updatedAt, 
    this.gaitSpeed
  }) : super._internal(
    id: id, 
    name: name, 
    family_name: family_name, 
    dob: dob, 
    gender: gender, 
    height_in_inches: height_in_inches, 
    Assessments: Assessments, 
    createdAt: createdAt, 
    updatedAt: updatedAt
  );

  factory GaitPatient({
    String? id,
    required String name,
    required String family_name,
    required TemporalDate dob,
    required Gender gender,
    required double height_in_inches,
    List<Assessment>? Assessments,
    TemporalDateTime? createdAt,
    TemporalDateTime? updatedAt,
    double? gaitSpeed
  }) {
    return GaitPatient._internal(
      id: id ?? UUID.getUUID(),
      name: name,
      family_name: family_name,
      dob: dob,
      gender: gender,
      height_in_inches: height_in_inches,
      Assessments: Assessments,
      createdAt: createdAt,
      updatedAt: updatedAt,
      gaitSpeed: gaitSpeed
    );
  }

  GaitPatient.fromJson(Map<String, dynamic> json) 
    : gaitSpeed = json['gaitSpeed'],
      super.fromJson(json);

  Map<String, dynamic> toJson() => {
    ...super.toJson(),
    'gaitSpeed': gaitSpeed,
  };
}

<!-- 结束代码段 -->
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

@immutable
class GaitPatient extends Patient {
final double? gaitSpeed;
const GaitPatient._internal({
required String id, 
required String name, 
required String family_name, 
required TemporalDate dob, 
required Gender gender, 
required double height_in_inches, 
List&lt;Assessment&gt;? Assessments, 
TemporalDateTime? createdAt, 
TemporalDateTime? updatedAt, 
this.gaitSpeed
}) : super._internal(
id: id, 
name: name, 
family_name: family_name, 
dob: dob, 
gender: gender, 
height_in_inches: height_in_inches, 
Assessments: Assessments, 
createdAt: createdAt, 
updatedAt: updatedAt
);
factory GaitPatient({
String? id,
required String name,
required String family_name,
required TemporalDate dob,
required Gender gender,
required double height_in_inches,
List&lt;Assessment&gt;? Assessments,
TemporalDateTime? createdAt,
TemporalDateTime? updatedAt,
double? gaitSpeed
}) {
return GaitPatient._internal(
id: id ?? UUID.getUUID(),
name: name,
family_name: family_name,
dob: dob,
gender: gender,
height_in_inches: height_in_inches,
Assessments: Assessments,
createdAt: createdAt,
updatedAt: updatedAt,
gaitSpeed: gaitSpeed
);
}
GaitPatient.fromJson(Map&lt;String, dynamic&gt; json) 
: gaitSpeed = json[&#39;gaitSpeed&#39;],
super.fromJson(json);
Map&lt;String, dynamic&gt; toJson() =&gt; {
...super.toJson(),
&#39;gaitSpeed&#39;: gaitSpeed,
};
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月16日 12:44:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定