Freezed: Superinterfaces don't have a valid override for 'copyWith': _$Tiny.copyWith (copyWith), _$Small.copyWith (copyWith).

Created on 24 Apr 2020  ·  7Comments  ·  Source: rrousselGit/freezed

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

Most helpful comment

Actually this could be done, by generating the map/when/co as extensions instead of inside _$Tiny

All 7 comments

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;
}
Was this page helpful?
0 / 5 - 0 ratings