Dart 2.19.0 Introduces dateTime.copyWith
On 20/10/22 a new version of Dart programming was released. It includes a lot of breaking changes and fixes. Today I will only describe one new feature that caught my attention. You can review the rest of changes by the link at the end of article.
Copying DateTime With Changes
Often, you need to copy DateTime while changing one or more elements.
For example when you show a graph displaying a range of sensor strength. User can choose a month, year or day. To calculate the startDate, you get the current DateTime, and then minus one from month, year or day.
In that case or others, it is useful to copy a date, and than change only one element like day, from today to yesterday, or to given date.
Patrick Cornelißen proposed including a core method handling that use case, because he often relies on his own helper method.
The proposal was discussed, and Lasse Nielsen implemented the feature meaning that since version 2.19.0 Dart comes with dateTime.copyWith method. Here’s a glimpse of how it works: