add method Null safety

dynamic add(
  1. dynamic executedBefore
)

Implementation

dynamic add(dynamic executedBefore) {
  final yearAmount = (unit.contains('year')
          ? amount
          : unit.contains('month')
              ? (amount / 12).truncate()
              : 0)
      .toInt();
  final monthAmount =
      (unit.contains('month') ? amount.remainder(12) : 0).toInt();
  final dayAmount = (unit.contains('week')
          ? amount * 7
          : unit.contains('day')
              ? amount
              : 0)
      .toInt();
  final hourAmount = (unit.contains('hour') ? amount : 0).toInt();
  final minuteAmount = (unit.contains('minut') ? amount : 0).toInt();
  final secondAmount =
      (unit.contains('second') && !unit.contains('millisecond') ? amount : 0)
          .toInt();
  final millisecondAmount =
      (unit.contains('millisecond') ? amount : 0).toInt();
  if ((executedBefore is Date &&
          (hourAmount != 0 ||
              minuteAmount != 0 ||
              secondAmount != 0 ||
              millisecondAmount != 0)) ||
      (executedBefore is Time &&
          (yearAmount != 0 || monthAmount != 0 || dayAmount != 0))) {
    throw FhirPathEvaluationException(
      'Date & Time additions must be done with the proper units.\n'
      '$executedBefore + $amount $unit was attempted, this is invalid',
      cause: executedBefore,
      operation: '+',
      arguments: [executedBefore, this],
    );
  }
  if (executedBefore is Date &&
      executedBefore.isValid &&
      executedBefore.value != null) {
    final newDate = DateTime(
        executedBefore.value!.year + yearAmount,
        executedBefore.value!.month + monthAmount,
        executedBefore.value!.day + dayAmount);
    if (executedBefore.precision == DatePrecision.YYYY) {
      return Date(newDate.toString().substring(0, 4));
    } else if (executedBefore.precision == DatePrecision.YYYYMM) {
      return Date(newDate.toString().substring(0, 7));
    } else {
      return Date(newDate.toString().substring(0, 10));
    }
  } else if (executedBefore is Time &&
      executedBefore.isValid &&
      executedBefore.value != null) {
    final timeList = executedBefore.value!.split(':');
    final duration = Duration(
      hours: int.tryParse(timeList.first) ?? 0 + hourAmount,
      minutes: (timeList.length > 1 ? int.tryParse(timeList[1]) ?? 0 : 0) +
          minuteAmount,
      seconds: (timeList.length > 2
              ? int.tryParse(timeList[2].split('.').first) ?? 0
              : 0) +
          secondAmount,
      milliseconds: (timeList.length > 2 && timeList[2].split('.').length > 1
              ? int.tryParse(timeList[2].split('.').last) ?? 0
              : 0) +
          millisecondAmount,
    );
    final durationList = duration.toString().split(':');
    durationList.first =
        int.parse(durationList.first).remainder(24).toString();
    return Time(durationList.join(':'));
  } else if (executedBefore is FhirDateTime &&
      executedBefore.isValid &&
      executedBefore.value != null) {
    final oldDateTime = executedBefore.value!;
    final newDateTime = DateTime(
      oldDateTime.year + yearAmount,
      oldDateTime.month + monthAmount,
      oldDateTime.day + dayAmount,
      oldDateTime.hour + hourAmount,
      oldDateTime.minute + minuteAmount,
      oldDateTime.second + secondAmount,
      oldDateTime.millisecond + millisecondAmount,
    );
    if (executedBefore.precision == DateTimePrecision.YYYY) {
      return FhirDateTime(newDateTime.toString().substring(0, 4));
    } else if (executedBefore.precision == DateTimePrecision.YYYYMM) {
      return FhirDateTime(newDateTime.toString().substring(0, 7));
    } else if (executedBefore.precision == DateTimePrecision.YYYYMMDD) {
      return FhirDateTime(newDateTime.toString().substring(0, 10));
    } else {
      return FhirDateTime(newDateTime);
    }
  }
}