Skip to content

Entities

Dustin Catap edited this page Jun 19, 2024 · 2 revisions

Entities are objects that represent the fundamental concepts of the problem domain and encapsulate business logic.

Below is an example of an entity class:

import 'package:equatable/equatable.dart';

class PostEntity with EquatableMixin {
  static const PostEntity empty = PostEntity(userId: 0, id: 0, title: '', body: '');

  const PostEntity({required this.userId, required this.id, required this.title, required this.body});

  final int userId;

  final int id;

  final String title;

  final String body;

  @override
  List<Object> get props => <Object>[userId, id, title, body];
}
  • We use EquatableMixin to make the entity comparable. When using this, we should make the class immutable by using final and const constructor.
  • We can also define a static empty instance of the entity. This is useful when we want to initialize the entity with empty values. As opposed to checking an instance of PostEntity for null, we can check if it is equal to `PostEntity.empty. This eliminates the need for null checks.