Google celebrates Hispanic Heritage Month. See how.

OptionalNullable<T> class

A wrapper type for optional values that may also be null. This generic class can be used in argument lists for optional arguments. When called, the argument state will be one of:

  • undefined - The caller did not provide a new value
  • Some(value) - The caller provided a new, non-null value
  • None() - The caller provided a null value

A common usage is:

class MyClass {
  String? maybeStr;
  List<int>? maybeList;
  Map<String, String>? maybeMap;

  MyClass({this.maybeStr, this.maybeList, this.maybeMap});

  MyClass cloneWithOverrides({
    OptionalNullable<String> maybeStr = const OptionalNullable.undefined(),
    OptionalNullable<List<int>> maybeList = const OptionalNullable.undefined(),
    OptionalNullable<Map<String, String>> maybeMap = const OptionalNullable.undefined(),
  }) {
    return MyClass(
      maybeStr: maybeStr.or(this.maybeStr),
      maybeList: maybeList.or(this.maybeList),
      maybeMap: maybeMap.or(this.maybeMap),
    );
  }
}

main() {
  final orig = MyClass(
    maybeStr: null,
    maybeList: [1, 2, 3],
    maybeMap: {'door': 'wood', 'window': 'glass'},
  );
  final mod = orig.cloneWithOverrides(
    maybeStr: Some('a string'),
    maybeMap: None(),
  );
  assert(mod.maybeStr == 'a string');
  assert(mod.maybeList!.length == 3);
  assert(mod.maybeMap == null);
}

Implementers

Constructors

OptionalNullable.undefined ()

Initialize an OptionalNullable to an undefined state. The value of an OptionalNullable is considered "undefined" until overridden by assigning it to Some or None. const

Properties

hashCode → int

The hash code for this object.
read-onlyinherited

isDefined → bool

True if the value is Some or None.
read-only

isUndefined → bool

True if the value is not Some or None.
read-only

runtimeType → Type

A representation of the runtime type of the object.
read-onlyinherited

Methods

noSuchMethod(Invocation invocation) dynamic

Invoked when a non-existent method or property is accessed.
inherited

or(T fallback) T

If the value is Some, the value is returned. If None, null is returned. Otherwise, the value isUndefined, in which case the given fallback value is returned instead.

toString() String

A string representation of this object.
inherited

Operators

operator ==(Object other) bool

The equality operator.
inherited