elementAtOrElse method

T elementAtOrElse(
  1. int index,
  2. T orElse()
)

Returns the indexth element. If the index is out of bounds the orElse supplier function is called to provide a value.

Example:

['a', 'b'].elementAtOrElse(2, () => ''); // ''

Implementation

T elementAtOrElse(int index, T Function() orElse) {
  RangeError.checkNotNegative(index, 'index');
  ArgumentError.checkNotNull(index);
  ArgumentError.checkNotNull(orElse, 'orElse');
  try {
    return elementAt(index);
  } catch (error) {
    return orElse();
  }
}