Records destructuring.
I was a bit confused with the syntax how to destructure/split the record fields into separate variables (similar to here), and I think it’s quite useful to refresh this one. In the example below you can use entire record boundaries
but if you only care about each field, you can assign it to local variables dayStart
and dayEnd
. I never know what is the order and suspect there might be others like me
void main() {
final boundaries = getDayBoundaries(DateTime.now());
print('Day start: ${boundaries.start}, day end: ${boundaries.end}');
final (start: dayStart, end: dayEnd) = getDayBoundaries(DateTime.now());
print('Day start: ${dayStart}, day end: ${dayEnd}');
}
({DateTime start, DateTime end}) getDayBoundaries(DateTime day) {
return (
start: DateTime(day.year, day.month, day.day, 0),
end: DateTime(day.year, day.month, day.day, 23, 59, 59, 999, 999)
);
}