Iso<A extends Object?, B extends Object?> class final

An isomorphism: a reversible transformation between two types.

An Iso represents a bidirectional mapping between types A and B. It consists of two inverse functions:

  • to: transforms from A to B
  • from: transforms from B to A

These functions form an isomorphism, meaning they are perfect inverses of each other: from(to(a)) == a and to(from(b)) == b for all valid values. This property is assumed but not enforced by the type system.

Use cases:

  • Type conversions with guaranteed round-trip safety
  • Bidirectional serialization/deserialization
  • Format conversions (e.g., JSON ↔ Dart objects)
  • Domain modeling (e.g., string representations ↔ enums)

Example:

// Define an isomorphism between String and int
final stringToInt = Iso(
  to: (s) => int.parse(s),
  from: (i) => i.toString(),
);

// Use it
assert(stringToInt.to('30') == 30);
assert(stringToInt.from(30) == '30');
assert(stringToInt.from(stringToInt.to('30')) == '30'); // Round-trip
Annotations
  • @experimental

Constructors

Iso({required B to(A), required A from(B)})
Creates an isomorphism with the given to and from transformations.
const

Properties

asLens Lens<A, B>
Downgrades this isomorphism to a Lens.
no setter
from → A Function(B)
Transforms a value from type B to type A.
final
hashCode int
The hash code for this object.
no setterinherited
inverse Iso<B, A>
Flips the direction of this isomorphism.
no setter
isoList Iso<List<A>, List<B>>
Lifts this isomorphism to work on lists of values.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
to → B Function(A)
Transforms a value from type A to type B.
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
then<C>(Iso<B, C> other) Iso<A, C>
Composes this isomorphism with another isomorphism.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited