JsonModelReader class

Reads typed values from a decoded-JSON object, accumulating a ValidationErrors list for missing or wrong-typed fields instead of throwing on the first problem.

The usual Map<String, dynamic> → model conversion either throws (losing every error after the first) or silently coerces (hiding bad data). This reader does neither: each requireX records a structured error and returns null, so a caller can build the model, then inspect errors once and report every field that failed at the same time.

Example:

final r = JsonModelReader(decoded);
final user = (name: r.requireString('name'), age: r.requireInt('age'));
if (r.errors.isNotEmpty) handle(r.errors.errors);

Constructors

JsonModelReader(Object? source, {String path = ''})
Wraps source, the decoded JSON object to read. A non-map source (null, a list, a scalar) is treated as an empty object, so every required read reports a missing-field error rather than throwing a cast error. path prefixes the ValidationErrorUtils.path of each error, letting a nested reader report address.city instead of a bare city. Audited: 2026-06-12 11:26 EDT

Properties

errors ValidationErrors
Accumulated errors for every failed read on this object. Audited: 2026-06-12 11:26 EDT
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

child(String key) JsonModelReader?
Required nested object as a child reader sharing this reader's error list, so a nested failure surfaces with a dotted _at path on the SAME errors collection. Returns null (and records an error) when absent or not a map. Audited: 2026-06-12 11:26 EDT
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
optionalString(String key, {String? fallback}) String?
Optional String: returns fallback when the key is absent, but still records an error when the key is present with a non-string value (bad data is a real problem; a missing optional field is not). Audited: 2026-06-12 11:26 EDT
requireBool(String key) bool?
Required bool; records an error and returns null if absent/non-bool. Audited: 2026-06-12 11:26 EDT
requireDouble(String key) double?
Required number as double; an int is widened. Records an error and returns null when the value is absent or not a number. Audited: 2026-06-12 11:26 EDT
requireInt(String key) int?
Required int; records an error and returns null if absent/non-int. Audited: 2026-06-12 11:26 EDT
requireList<E>(String key) List<E>?
Required homogeneous list. Records an error and returns null when the value is absent, is not a list, or any element is not an E. Audited: 2026-06-12 11:26 EDT
requireString(String key) String?
Required String; records an error and returns null if absent/non-string. Audited: 2026-06-12 11:26 EDT
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited