Freezed: Inherit from a freezed data class into another freezed data class

Created on 20 Oct 2020  路  3Comments  路  Source: rrousselGit/freezed

Hi @rrousselGit

* Inherit from a freezed data class into another freezed data class*
What I have is a base FileModel like below:

@freezed
abstract class FileModel with _$FileModel {
  const factory FileModel({
    @required int type,
    @required int size,
    @required int pk,
  }) = _FileModel;

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

and then I have some classes which inherit FileModel fields like:

@freezed
abstract class CommentFileModel with _$CommentFileModel {
  const factory CommentFileModel({
    @required String resourcetype,
    @required DateTime createdAt,
    @required String blurHash,
    @required int duration,
    @required String title,
    @required String link,
  }) = _CommentFileModel;

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

now, I want to know how it is possible to inherit from a freezed data class into another freezed data class.
(CommentFileModel from FileModel)
Thanks.

documentation needs triage

All 3 comments

You cannot use Inheritance for Freezed classes.

so, I have to use normal dart class ?

You can use composition

Make CustomFileModel receive a FileModel in its constructor

Was this page helpful?
0 / 5 - 0 ratings