我有一个像这样的普通班
enum FamilyRelationEnum { father, mother, brother, sister }
class FamilyMemberResponse {
FamilyMemberResponse(this.name, this.relation);
final String name, relation;
FamilyRelationEnum? _relationEnum;
FamilyRelationEnum get relationEnum => _relationEnum ??=
FamilyRelationEnum.values.firstWhere((e) => e.name == relation);
}
我只想生成它的冻结版本。这是我到目前为止所尝试的......
@freezed
class FreezedFamilyMemberResponse with _$FreezedFamilyMemberResponse {
const FreezedFamilyMemberResponse._();
const factory FreezedFamilyMemberResponse(String? name, String? relation) =
_FreezedFamilyMemberResponse;
factory FreezedFamilyMemberResponse.fromJson(Map<String, dynamic> json) =>
_$FreezedFamilyMemberResponseFromJson(json);
FamilyRelationEnum? _relationEnum;
FamilyRelationEnum get relationEnum => _relationEnum ??=
FamilyRelationEnum.values.firstWhere((e) => e.name == relation);
}
由于非 final 字段,无法完成代码生成。或者有没有更好的解决方案来在构建时映射枚举。提前致谢 :)