requireDateTime function

DateTime requireDateTime(
  1. Map<String, dynamic> json,
  2. String key,
  3. String what
)

The DateTime at key, or a FormatException naming the field.

Separate from requireString because an unparseable timestamp fails in DateTime.parse rather than in the cast, which produced the same anonymous FormatException: Invalid date format from inside the SDK.

Implementation

DateTime requireDateTime(Map<String, dynamic> json, String key, String what) {
  final value = requireString(json, key, what);

  final parsed = DateTime.tryParse(value);
  if (parsed != null) return parsed;

  throw FormatException(
    '$what expected "$key" to be an ISO 8601 date, got: $value',
  );
}