max property

  1. @useResult
E? get max

The maximum element in this iterable or null if empty. Returns double.nan if present.

[1, 2, 3].max; // 3

[1, 2, double.nan].max; // double.nan

Implementation

@useResult E? get max {
  final iterator = this.iterator;
  if (!iterator.moveNext()) {
    return null;
  }

  var max = iterator.current;
  if (max.isNaN) {
    return max;
  }

  while (iterator.moveNext()) {
    final element = iterator.current;
    if (element.isNaN) {
      return element;
    }

    if (max.compareTo(element) < 0) {
      max = element;
    }
  }

  return max;
}