list_ext.dart 512 Bytes


extension ListExtension<E> on List<E> {
  /// 获取数组中第一个匹配元素的index,没有就返回null
  int? indexWhereOrNull(bool Function(E element) test) {
    for (int i = 0; i < length; i++) {
      if (test(this[i])) return i;
    }
    return null;
  }

  /// 获取数组中第一个匹配元素,没有就返回null
  E? firstWhereOrNull(bool Function(E element) test) {
    for (E element in this) {
      if (test(element)) {
        return element;
      }
    }
    return null;
  }
}