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
  1. Union of Modddels

Case-modddels pattern matching

PreviousBasic UsageNextDefault Factory Constructor

Let's continue with our Weather example .

The super-sealed classes have additional "special" pattern matching methods that allow you to map between the different modddels. In our example, these methods are : mapWeather, maybeMapWeather and mapOrNullWeather.

For Example :

weather.mapWeather(
  sunny: (Sunny sunny) {},
  rainy: (Rainy rainy) {},
);

weather.mapOrNullWeather(
  rainy: (Rainy rainy) {},
);

weather.maybeMapWeather(
  sunny: (Sunny sunny) {},
  orElse: () {},
);

In general, these methods are named 'map{ModddelName}', 'maybeMap{ModddelName}' and 'mapOrNull{ModddelName}'.

These methods can be used on all the super-sealed classes :

Super-sealed class
Case-Modddels you can map between

Weather

Sunny - Rainy

ValidWeather

ValidSunny - ValidRainy

InvalidWeather

InvalidSunny - InvalidRainy

InvalidWeatherValue

InvalidSunnyValue - InvalidRainyValue

For Example :

weather.mapValidity(
  valid: (ValidWeather validWeather) {
    // Using `mapWeather` on `ValidWeather`
    validWeather.mapWeather(
      sunny: (ValidSunny validSunny) {},
      rainy: (ValidRainy validRainy) {},
    );
  },
  invalid: (InvalidWeather invalidWeather) {
    // Using `mapWeather` on `InvalidWeather`
    invalidWeather.mapWeather(
      sunny: (InvalidSunny invalidSunny) {},
      rainy: (InvalidRainy invalidRainy) {},
    );
  },
);

Notice how the type of the callback parameters changes according to which super-sealed class the mapWeather method is called on.

(find it here)