Modddels
HomeGithubPub.dev
v0.2
v0.2
  • Motivation
  • Setup
  • Generalities
    • Modddels Overview
    • Validations & Validation steps
    • Structure of a Modddel
    • Member parameters & Dependency parameters
    • Usage
      • Data equality & toString
      • Pattern matching
      • Reading the fields
      • Reading the failures
      • CopyWith
  • ValueObjects
    • ValueObjects Overview
    • Creating a ValueObject
  • Entities
    • Entities Overview
    • ContentValidation & ContentFailure
    • ValidationSteps in Entities
    • SimpleEntity
      • Creating a SimpleEntity
      • Special Cases
    • IterableEntity & Iterable2Entity
      • IterableEntity / Iterable2Entity kinds
      • The Type Template
      • Creating an IterableEntity / Iterable2Entity
      • InvalidMembers Description
      • Special Cases
      • Creating your own IterableEntity / Iterable2Entity kind
  • Advanced Notions
    • The NullFailure Annotation
    • Using multiple parameters annotations
    • Class Hierarchy of ValueObjects
    • Class Hierarchy of Entities
    • Models that are always valid / invalid
  • Union of Modddels
    • Union of Modddels Overview
    • Basic Usage
    • Case-modddels pattern matching
    • Default Factory Constructor
    • Shared Properties
    • The Validate Methods
    • CopyWith
  • Unit-Testing Of Modddels
    • Unit-Testing Overview
    • Available Tests
    • Customizing Tests
  • Additional Information
    • Using type aliases
    • Re-running the generator
    • All Available Modddels
    • VsCode Snippets
Powered by GitBook
On this page
  • Steps to create an IterableEntity / Iterable2Entity
  • Complete Example
  1. Entities
  2. IterableEntity & Iterable2Entity

Creating an IterableEntity / Iterable2Entity

PreviousThe Type TemplateNextInvalidMembers Description

Steps to create an IterableEntity / Iterable2Entity

Declare your class this way :

/// ListEntity example
///
/// This will hold a list of [Todo]s
class TodoList extends ListEntity<InvalidTodoList, ValidTodoList> 
    with _$TodoList {}

/// MapEntity example
///
/// This will hold a map where the keys are [Author]s and values 
/// are [Book]s
class BookMap extends MapEntity<InvalidBookMap, ValidBookMap> 
    with _$BookMap {}

Add the , the (don't forget including the contentValidation), and the .

Add the . The factory constructor should have only one member parameter, which type should match the .

/// ListEntity example
//...
factory TodoList({
  required List<Todo> todos,
}) //...

/// MapEntity example
//...
factory BookMap({
  required Map<Author, Book> booksByAuthors,
}) //...

When using some Dartz collections (ISetEntity, IMapEntity...), you also need to override the $getOrder method.

Complete Example

// ... Imports & Part statements

@Modddel(
  validationSteps: [
    ValidationStep([
      contentValidation,
      Validation('size', FailureType<TodoListSizeFailure>()),
    ]),
  ],
)
class TodoList extends ListEntity<InvalidTodoList, ValidTodoList>
    with _$TodoList {
  TodoList._();

  factory TodoList({
    required List<Todo> todos,
  }) {
    return _$TodoList._create(
      todos: todos,
    );
  }

  @override
  Option<TodoListSizeFailure> validateSize(todoList) {
    if (todoList.todos.isEmpty) {
      return some(const TodoListSizeFailure.empty());
    }
    return none();
  }
}

@freezed
class TodoListSizeFailure extends EntityFailure with _$TodoListSizeFailure {
  const factory TodoListSizeFailure.empty() = _Empty;
}

Create your .

You can now run the generator, and then override and implement the . Note that the "validate" method of the contentValidation is automatically generated, and you should never override it (Your IDE's Quick Fix won't override it neither).

type template
imports and part statements
@Modddel annotation
private empty constructor
factory constructor
failure(s) sealed class(es)
"validate" methods