How can I use freezed with subclasses?
@freezed
abstract class Tiny with _$Tiny {
factory Tiny({ String name }) = _Tiny;
}
@freezed
abstract class Small with _$Small implements Tiny {
factory Small({ String name, int age }) = _Small;
}
You can't.
Freezed classes are sealed
@dened I solved that by creating another abstract class and implement it in all my freezed classes, so:
abstract class Base {}
@freezed
abstract class Tiny with _$Tiny implements Base {
factory Tiny({ String name }) = _Tiny;
}
@freezed
abstract class Small with _$Small implements Base {
factory Small({ String name, int age }) = _Small;
}
// Used as follow:
void main() {
Base small= Small();
Base tiny= Tiny();
}
@rrousselGit what do you think about the @pedromassango's solution?
It works
Actually this could be done, by generating the map/when/co as extensions instead of inside _$Tiny
@rrousselGit are you planing to add support for this? Is is really very useful
Actually, after more investigations, it's not possible.
when/map/co have to be methods as Freezed relies on overriding their implementation for copyWith(foo: null) to work. So I'll close this in the lack of a solution.
But you can make a normal interface and implement it:
abstract class Tiny {
String get name;
}
@freezed
abstract class Small with _$Small implements Tiny {
factory Small({ String name, int age }) = _Small;
}
Most helpful comment
Actually this could be done, by generating the map/when/co as extensions instead of inside
_$Tiny