elementAtAsOptional method

Optional<T> elementAtAsOptional(
  1. int index
)

Returns the indexth element wrapped by an Optional if this element exists, otherwise returns Optional.empty.

If the index passed is invalid, Optional.empty is returned as well.

Index zero represents the first element (so elementAt(0) is equivalent to first).

This lookup can not distinguish between handling and invalid index and containing the value null at the requested index. Methods like contains or length can be used if the distinction is important.

Implementation

Optional<T> elementAtAsOptional(int index) =>
    getValueIf(_isValidIndex(index), () => _list.elementAt(index));