Optional<T> class

A value that might be absent.

This class was written before Dart SDK 2.12 added first class support for non-nullable types. Where possible, it is strongly preferred to use those language features and to not use this class at all.

Usually, Optional<T> should be migrated to T?.

However, code that already uses Optional can continue to use it if the cost of migration is considered too high. The class will not be removed.

Valid new use cases:

  • if an API only accepts non-null values, you can use Optional as a general way to pass it values that mean "absent";
  • for a general way to represent "nested levels" of absent, such as Optional<Optional<T>>.

Both of these should be very rare.

Inheritance
Available extensions
Annotations
  • @Deprecated('Generally, migrate to a nullable type.')

Constructors

Optional.absent()
Constructs an empty Optional.
const
Optional.fromNullable(T? value)
Constructs an Optional of the given value.
const
Optional.of(T value)
Constructs an Optional of the given value.

Properties

first → T
The first element.
no setterinherited
firstOrNull → T?

Available on Iterable<T>, provided by the IterableExtensions extension

The first element of this iterator, or null if the iterable is empty.
no setter
hashCode int
Delegates to the underlying value hashCode.
no setteroverride
indexed Iterable<(int, T)>

Available on Iterable<T>, provided by the IterableExtensions extension

Pairs of elements of the indices and elements of this iterable.
no setter
isEmpty bool
Whether this collection has no elements.
no setterinherited
isNotEmpty bool
Whether this collection has at least one element.
no setterinherited
isNotPresent bool
True when this optional contains no value.
no setter
isPresent bool
True when this optional contains a value.
no setter
iterator Iterator<T>
A new Iterator that allows iterating the elements of this Iterable.
no setteroverride
last → T
The last element.
no setterinherited
lastOrNull → T?

Available on Iterable<T>, provided by the IterableExtensions extension

The last element of this iterable, or null if the iterable is empty.
no setter
length int
The number of elements in this Iterable.
no setterinherited
nonNulls Iterable<T>

Available on Iterable<T?>, provided by the NullableIterableExtensions extension

The non-null elements of this iterable.
no setter
orNull → T?
Gets the Optional value, or null if there is none.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
single → T
Checks that this iterable has only one element, and returns that element.
no setterinherited
singleOrNull → T?

Available on Iterable<T>, provided by the IterableExtensions extension

The single element of this iterator, or null.
no setter
value → T
Gets the Optional value.
no setter
wait Future<List<T>>

Available on Iterable<Future<T>>, provided by the FutureIterable extension

Waits for futures in parallel.
no setter

Methods

any(bool test(T element)) bool
Checks whether any element of this iterable satisfies test.
inherited
asNameMap() Map<String, T>

Available on Iterable<T>, provided by the EnumByName extension

Creates a map from the names of enum values to the values.
byName(String name) → T

Available on Iterable<T>, provided by the EnumByName extension

Finds the enum value in this list with name name.
cast<R>() Iterable<R>
A view of this iterable as an iterable of R instances.
inherited
contains(Object? element) bool
Whether the collection contains an element equal to element.
inherited
elementAt(int index) → T
Returns the indexth element.
inherited
elementAtOrNull(int index) → T?

Available on Iterable<T>, provided by the IterableExtensions extension

The element at position index of this iterable, or null.
every(bool test(T element)) bool
Checks whether every element of this iterable satisfies test.
inherited
expand<T>(Iterable<T> toElements(T element)) Iterable<T>
Expands each element of this Iterable into zero or more elements.
inherited
firstWhere(bool test(T element), {T orElse()?}) → T
The first element that satisfies the given predicate test.
inherited
fold<T>(T initialValue, T combine(T previousValue, T element)) → T
Reduces a collection to a single value by iteratively combining each element of the collection with an existing value
inherited
followedBy(Iterable<T> other) Iterable<T>
Creates the lazy concatenation of this iterable and other.
inherited
forEach(void action(T element)) → void
Invokes action on each element of this iterable in iteration order.
inherited
ifAbsent(void ifAbsent()) → void
Execution a function if the Optional value is absent.
ifPresent(void ifPresent(T value)) → void
Executes a function if the Optional value is present.
join([String separator = ""]) String
Converts each element to a String and concatenates the strings.
inherited
lastWhere(bool test(T element), {T orElse()?}) → T
The last element that satisfies the given predicate test.
inherited
map<T>(T toElement(T e)) Iterable<T>
The current elements of this iterable modified by toElement.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
or(T defaultValue) → T
Gets the Optional value with a default.
reduce(T combine(T value, T element)) → T
Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.
inherited
singleWhere(bool test(T element), {T orElse()?}) → T
The single element that satisfies test.
inherited
skip(int count) Iterable<T>
Creates an Iterable that provides all but the first count elements.
inherited
skipWhile(bool test(T value)) Iterable<T>
Creates an Iterable that skips leading elements while test is satisfied.
inherited
take(int count) Iterable<T>
Creates a lazy iterable of the count first elements of this iterable.
inherited
takeWhile(bool test(T value)) Iterable<T>
Creates a lazy iterable of the leading elements satisfying test.
inherited
toList({bool growable = true}) List<T>
Creates a List containing the elements of this Iterable.
inherited
toSet() Set<T>
Creates a Set containing the same elements as this iterable.
inherited
toString() String
Returns a string representation of (some of) the elements of this.
override
transform<S>(S transformer(T value)) Optional<S>
Transforms the Optional value.
transformNullable<S>(S? transformer(T value)) Optional<S>
Transforms the Optional value.
where(bool test(T element)) Iterable<T>
Creates a new lazy Iterable with all elements that satisfy the predicate test.
inherited
whereType<T>() Iterable<T>
Creates a new lazy Iterable with all elements that have type T.
inherited

Operators

operator ==(Object o) bool
Delegates to the underlying value operator==.
override