4

我对 Flutter 很陌生(甚至对 Freezed 包也很陌生……)所以我希望这个问题是相关的。

所以,这是我的用例:用户可以是不同组的成员。一个用户类共享。我有问题要处理与 Freezed 的这种关系:

@freezed
abstract class User with _$User {
  const factory User({
    @required UniqueId id,
    Name firstname,
    Name lastname,
    @required EmailAddress email,
    @required bool emailVerified,
  }) = _User;
}

@freezed
abstract class Member extends User with _$Member { // Here is the issue
  const factory Member({
    @required UniqueId id,
    @required Name displayname,
    String photo,
    List roles,
    String status,
    DateTime expiration,
  }) = _Member;

_$Member.copyWith' ('$MemberCopyWith Function()') 不是 '_$User.copyWith' ('$UserCopyWith Function()') 的有效覆盖。

这样做的正确方法是什么?

4

1 回答 1

-1

您必须使用Implements标签并创建User一个普通的抽象类。但考虑到您的情况,我认为最好执行以下操作。

abstract class UserBase {
  UniqueId get id;
  Name get firstname;
  Name get lastname;
  EmailAddress get email;
  bool get emailVerified;
}

@freezed
abstract class User with _$User {
  @Implements(UserBase)
  const factory User({
    @required UniqueId id,
    Name firstname,
    Name lastname,
    @required EmailAddress email,
    @required bool emailVerified,
  }) = _User;
}

@freezed
abstract class Member with _$Member {
  @Implements(UserBase)
  const factory Member({
    @required UniqueId id,
    Name firstname,
    Name lastname,
    @required EmailAddress email,
    @required bool emailVerified,
    @required Name displayname,
    String photo,
    List roles,
    String status,
    DateTime expiration,
  }) = _Member;
}

随时提出任何疑问。

于 2020-12-12T08:31:52.650 回答