average property

double? get average

Average (mean). Returns null if empty.

Iterates the source exactly once, so lazy iterables are safe. Audited: 2026-06-12 11:26 EDT

Implementation

double? get average {
  // Materialize once and take BOTH the sum and the count from this copy:
  // summing `this` re-ran a lazy source and could mix two different data sets.
  final List<num> list = toList();
  // Empty has no mean; null avoids returning 0/0 (NaN).
  if (list.isEmpty) return null;
  return list.sum.toDouble() / list.length;
}