differenceToTimeOfDay method
Safely calculates the difference between two nullable DateTime objects as TimeOfDay Returns null if either this or other is null, otherwise returns the TimeOfDay difference
Implementation
TimeOfDay? differenceToTimeOfDay(DateTime? other) {
if (this != null && other != null) {
final duration = this!.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;
}