addDuration function Null safety

dynamic addDuration(
  1. dynamic date,
  2. Duration duration
)

Add the date value with duration without daylight saving value

eg., if local time zone as British Summer Time, and its daylight saving enabled on 29 March, 2020 to 25 October, 2020. add one day to the date(Oct 25, 2020) using add() method in DateTime, it return Oct 25, 2020 23.00.00 instead of Oct 26, 2020 because this method consider location and daylight saving.

So check whether the current date time zone offset(+1) is equal to previous date time zone offset(+2). if both are not equal then calculate the difference (date.timeZoneOffset - currentDate.timeZoneOffset) and add the offset difference(2-1) to current date.

Implementation

dynamic addDuration(dynamic date, Duration duration) {
  dynamic currentDate = date.add(duration);
  if (date.timeZoneOffset != currentDate.timeZoneOffset) {
    currentDate =
        currentDate.add(date.timeZoneOffset - currentDate.timeZoneOffset);
  }

  return currentDate;
}