How to inherit from a parent class with Freezed
Asked Answered
S

1

7

I'm quite new to Flutter (and even more to the Freezed package...) So I hope that the question is relevant.

So, here is my usecase: a User can be Member of differents groups. A User class shares . I have issues to deal with this relationship with 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()') isn't a valid override of '_$User.copyWith' ('$UserCopyWith Function()').

What would be the right way to do so?

Specialist answered 24/10, 2020 at 15:52 Comment(0)
A
-3

You'll have to use the Implements tag and make User a normal abstract class. But considering your scenario, I think it's best to do the following.

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;
}

Feel free to ask any queries.

Alyssa answered 12/12, 2020 at 8:31 Comment(2)
I'm having troubles trying to inherit a method from a parent class using Freezed. This doesn't help me with that, do you have any suggestion?Windermere
@LuisUtrera Idk exactly what your trouble is. Maybe this could help? https://mcmap.net/q/1630282/-flutter-error-implementing-interface-with-freezedAlyssa

© 2022 - 2024 — McMap. All rights reserved.