ValueOrAbsent<T> class sealed

Distinguishes an omitted copy value from an explicit replacement value.

This is useful for copyWith methods where a nullable field must be able to keep its current value, change to a non-null value, or change to null.

class Example {
  const Example({this.name});

  final String? name;

  Example copyWith({
    ValueOrAbsent<String?> name = const ValueOrAbsent.absent(),
  }) =>
      Example(name: name.valueOr(this.name));
}

example.copyWith(); // keep existing name
example.copyWith(name: ValueOrAbsent.value('room')); // set name
example.copyWith(name: ValueOrAbsent.value(null)); // clear name

Experimental: this API may change in a future release.

Annotations

Constructors

ValueOrAbsent.absent()
Creates an omitted value that preserves the current field.
const
factory
ValueOrAbsent.value(T value)
Creates an explicit replacement value.
const
factory

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited
valueOr(T other) → T
Returns the explicit value, or other when this value is absent.

Operators

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