avg method

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

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

Implementation

DateTime? avg(Iterable<DateTime?>? dates) {
  if (dates == null) return null;
  final filteredDates = dates.whereType<DateTime>();
  if (filteredDates.isEmpty) return null;
  final totalMs = filteredDates.fold<int>(
    0,
    (sum, date) => sum + date.millisecondsSinceEpoch,
  );
  final avgMs = totalMs ~/ filteredDates.length;
  return DateTime.fromMillisecondsSinceEpoch(avgMs);
}