I have 4 files
PostModel
which extends Post
(entity) &
PageModel
which extends Page
(entity)
A Post has List of pages in it and same I implemented for postModel which has list of pagemodels.
I'm getting an error '_$PostModel.pages' ('List Function()') isn't a valid override of 'Post.pages' ('List Function()').
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';
import 'package:**/features/home/data/models/page_model.dart';
import 'package:**/features/home/domain/entities/post.dart';
part 'post_model.freezed.dart';
part 'post_model.g.dart';
@freezed
class PostModel extends Post with _$PostModel {
const factory PostModel({
required String id,
required String title,
required String body,
required List<PageModel> pages,
}) = _PostModel;
factory PostModel.fromJson(Map<String, Object?> json) =>
_$PostModelFromJson(json);
}
Post Class here :
class Post {
final String id;
final String title;
final String body;
final List<Page> pages;
Post({
required this.id,
required this.title,
required this.body,
required this.pages,
} );
}
You won't be able to do that with freezed. You can look at the issues:
Inheritance is not supported
However, you could define an abstract class IPost
and use it as an interface:
abstract class IPost {
String get id;
String get title;
String get body;
List<Page> get pages;
}
class Post implements IPost {
Post({
required this.id,
required this.title,
required this.body,
required this.pages,
} );
@override
final String id;
@override
final String title;
@override
final String body;
@override
final List<Page> pages;
}
@freezed
class PostModel with _$PostModel implements IPost {
const factory PostModel({
required String id,
required String title,
required String body,
required List<Page> pages,
}) = _PostModel;
factory PostModel.fromJson(Map<String, Object?> json) =>
_$PostModelFromJson(json);
}