req<T> function

T req<T>(
  1. Map<String, dynamic> json,
  2. String key,
  3. String path
)

Reads a required key of type T, throwing with the JSON path if absent or of the wrong type.

dart:convert decodes a JSON whole number (e.g. 2) as int, never double. Read numeric fields as num or intT = double will throw on a valid whole-number value.

Implementation

T req<T>(Map<String, dynamic> json, String key, String path) {
  final value = json[key];
  if (value is T) return value;
  throw SchemaFormatException(
    _join(path, key),
    'expected $T, got ${value.runtimeType}',
  );
}