differenceWithAsTimeOfDay method

TimeOfDay? differenceWithAsTimeOfDay(
  1. DateTime? other
)

Safely calculates the difference with a nullable DateTime as TimeOfDay Returns null if other is null, otherwise returns the TimeOfDay difference

Implementation

TimeOfDay? differenceWithAsTimeOfDay(DateTime? other) {
  if (other != null) {
    final duration = difference(other);
    final totalMinutes = duration.inMinutes
        .abs(); // Use absolute value to handle negative differences
    final hours = totalMinutes ~/ 60;
    final minutes = totalMinutes % 60;
    return TimeOfDay(hour: hours, minute: minutes);
  }
  return null;
}