asDateTime method

DateTime? asDateTime({
  1. DateTime? def,
})

Converts the dynamic value to a DateTime object. If the value is null, returns the provided default DateTime or a DateTime set to UTC 1977. If the value is a String, it attempts to parse it into a DateTime. If parsing fails, returns null. def The default DateTime to return if the value is null. Returns the DateTime representation of the dynamic value or the default DateTime.

Implementation

DateTime? asDateTime({DateTime? def}) {
  var value = this ?? def;
  if (value == null) {
    return DateTime.utc(1977);
  }
  var res = DateTime.tryParse(value.toString().trim());
  return res;
}