我正在尝试拥有一个基本的 Freezed 接口,我的应用程序实体接口可以扩展它,以便我可以在接口上调用冻结的函数。我已经在这里开始了这个过程,到目前为止它似乎正在工作:
abstract class IUserRegistrationEntity<T> extends FreezedClass<T> {
String get nickName;
String get email;
String get confirmEmail;
String get password;
String get confirmPassword;
}
abstract class FreezedClass<T> {
T get copyWith;
Map<String, dynamic> toJson();
}
冻结类:
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:vepo/domain/user_registration/i_user_registration_entity.dart';
part 'user_registration_entity.freezed.dart';
part 'user_registration_entity.g.dart';
@freezed
abstract class UserRegistrationEntity with _$UserRegistrationEntity {
@Implements.fromString(
'IUserRegistrationEntity<\$UserRegistrationEntityCopyWith<IUserRegistrationEntity>>')
const factory UserRegistrationEntity(
{String nickName,
String email,
String confirmEmail,
String password,
String confirmPassword}) = _IUserRegistrationEntity;
factory UserRegistrationEntity.fromJson(Map<String, dynamic> json) =>
_$UserRegistrationEntityFromJson(json);
}
但是现在我需要将 fromJson 工厂构造函数添加到接口中。我认为这可能是我正在寻找的东西,尽管我真的不知道如何在我的代码中实现它:
T deserialize<T extends JsonSerializable>(
String json,
T factory(Map<String, dynamic> data),
) {
return factory(jsonDecode(json) as Map<String, dynamic>);
}
You an then call it with:
var myValue = deserialize(jsonString, (x) => MyClass.fromJson(x));
将 fromJson 添加到我的冻结界面的任何帮助将不胜感激。