英文:
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
的扩展类,你可以考虑以下选项:
- 显式构造函数: 你可以在
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.
}
- 使用工厂构造函数: 如果你决定继续使用工厂构造函数,可以尝试将
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<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;
...
}
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<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,
};
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论