4

I have read the documentation but still not able to understand how it works with polymorphism.

See the example code I wish to achieve:
abstract class BaseUser extends HiveObject {
  @HiveField(1)
  final String name;

  BaseUser({required this.name});
}

@freezed
@HiveType(typeId: 101)
class Employee with _$Employee {
  @Implements(BaseUser)
  factory Employee({
    required String name,
    @HiveField(2) int? salary,
  }) = _Employee;

  factory Employee.fromJson(Map<String, dynamic> json) => _$EmployeeFromJson(json);
}

@freezed
@HiveType(typeId: 102)
class Manager with _$Manager {
  @Implements(BaseUser)
  factory Manager({
    required String name,
    @HiveField(2) String? role,
  }) = _Manager;

  factory Manager.fromJson(Map<String, dynamic> json) => _$ManagerFromJson(json);
}

void main() {
  BaseUser ref = Employee(name: 'name');
}

Since,

You cannot use @With/@Implements with freezed classes. Freezed classes can neither be extended nor implemented.

What could be the possible way to achieve this?

padaleiana
  • 955
  • 1
  • 14
  • 23

0 Answers0