singleAsOptional property

Optional<T> singleAsOptional

Returns an Optional containing the only element of this list if it has exactly one element, otherwise returns Optional.empty.

This lookup can not distinguish between the list being empty and containing the null value as only element. Methods like contains or length can be used if the distinction is important.

Implementation

Optional<T> get singleAsOptional =>
    getValueIf(length == 1, () => _list.single);