elementAtOrElse method

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

Returns an element at the given index or the result of calling the defaultValue function if the index is out of bounds of this collection.

Implementation

T elementAtOrElse(int index, T Function(int) defaultValue) {
  if (index < 0) {
    return defaultValue(index);
  }
  final i = iterator();
  int count = 0;
  while (i.hasNext()) {
    final element = i.next();
    if (index == count++) {
      return element;
    }
  }
  return defaultValue(index);
}