elementAtOrNull method

E? elementAtOrNull(
  1. int index
)

Returns an element at the given index or null if the index is out of bounds of this collection.

final list = [1, 2, 3, 4];
final first = list.elementAtOrNull(0); // 1
final fifth = list.elementAtOrNull(4); // null

Implementation

E? elementAtOrNull(int index) {
  if (index < 0) return null;
  var count = 0;
  for (final element in this) {
    if (index == count++) return element;
  }
  return null;
}