median static method

DateTime? median(
  1. Iterable<DateTime?>? dates
)

Returns the median date in the list of dates, ignoring null values.

Implementation

static DateTime? median(Iterable<DateTime?>? dates) {
  if (dates == null) return null;
  final filteredDates = dates.whereType<DateTime>().toList()..sort();
  if (filteredDates.isEmpty) return null;
  final middleIndex = filteredDates.length ~/ 2;
  if (filteredDates.length.isOdd) {
    return filteredDates[middleIndex];
  } else {
    final medianMs = (filteredDates[middleIndex - 1].millisecondsSinceEpoch +
            filteredDates[middleIndex].millisecondsSinceEpoch) ~/
        2;
    return DateTime.fromMillisecondsSinceEpoch(medianMs);
  }
}