safeParse static method

DateTime? safeParse(
  1. String? str
)

Implementation

static DateTime? safeParse(String? str) {
  if (str == null) {
    return null;
  }

  const dateFormatPatterns = [
    'EEE, d MMM yyyy HH:mm:ss Z',
  ];

  // DateTime.parse returns null if the input has
  // trailing spaces. Remove the spaces to avoid that.
  final trimmedDate = str.trim();
  try {
    return DateTime.parse(trimmedDate);
  } catch (_) {
    for (final pattern in dateFormatPatterns) {
      try {
        final format = DateFormat(pattern);
        return format.parse(trimmedDate);
      } catch (_) {}
    }
  }
  return null;
}