indexOf<T> function

int indexOf<T>(
  1. Iterable<T> elements,
  2. bool predicate(
    1. T element
    )
)

Returns the index of the first item in elements where predicate evaluates to true.

Returns -1 if there are no items where predicate evaluates to true.

Implementation

int indexOf<T>(Iterable<T> elements, bool Function(T element) predicate) {
  if (elements is List<T>) {
    for (int i = 0; i < elements.length; i++) {
      if (predicate(elements[i])) return i;
    }
    return -1;
  }

  int i = 0;
  for (final element in elements) {
    if (predicate(element)) return i;
    i++;
  }
  return -1;
}