max<T> function Null safety

T? max<T>(
  1. Iterable<T> i,
  2. [Comparator<T>? compare]

Returns the maximum value in i, according to the order specified by the compare function, or null if i is empty.

The compare function must act as a Comparator. If compare is omitted, is used. If i contains null elements, an exception will be thrown.


T? max<T>(Iterable<T> i, [Comparator<T>? compare]) {
  if (i.isEmpty) return null;
  final Comparator<T> _compare = compare ?? _compareAny;
  return i.reduce((a, b) => _compare(a, b) > 0 ? a : b);