toDateTime method

DateTime? toDateTime()

Converts the String value to a DateTime object if it's valid.

Returns the DateTime object if the string can be parsed successfully, otherwise returns null.

Example:

String? dateStr = "2023-04-28T05:34:53.684Z";
print(dateStr.toDateTime()); // 2023-04-28 05:34:53.684Z

Implementation

DateTime? toDateTime() => isNotEmptyOrNull ? DateTime.tryParse(this!) : null;