Skip to content

OptionOps<A>

extensionOptionOps<A>onOption<A>

Additional combinators on Option.

Properties

get extension no setter

Agetget

Returns the value if this is a Some, or throws a StateError if this is a None.

This is an unsafe, partial operation. Prefer getOrElse, fold, or pattern matching when the Option may be None. Use get only when you have already proven that this Option is non-empty.

See also getOrNull for a nullable alternative, and getOrElse for providing a fallback value.

Available on Option<A>, provided by the OptionOps<A> extension

Implementation
dart
A get get => fold(() => throw StateError('None.get'), identity);

getOrNull extension no setter

A?getgetOrNull

Returns the value if this is a Some, or null if this is a None.

This is equivalent to Option.toNullable and is provided as a conventionally named companion to get.

Available on Option<A>, provided by the OptionOps<A> extension

Implementation
dart
A? get getOrNull => toNullable();

Methods

contains() extension

boolcontains(Aelem)

Available on Option<A>, provided by the OptionOps<A> extension

Implementation
dart
bool contains(A elem) => fold(() => false, (value) => value == elem);

getOrElse() extension

AgetOrElse(AFunction()ifEmpty)

Returns the value if this is a Some or the value returned from evaluating ifEmpty.

Available on Option<A>, provided by the OptionOps<A> extension

Implementation
dart
A getOrElse(Function0<A> ifEmpty) => fold(ifEmpty, identity);

orElse() extension

Option<A>orElse(Option<A>Function()orElse)

If this is a Some, this is returned, otherwise the result of evaluating orElse is returned.

Available on Option<A>, provided by the OptionOps<A> extension

Implementation
dart
Option<A> orElse(Function0<Option<A>> orElse) => fold(orElse, (_) => this);