JsonReader class

Typed accessors for the JSON maps that form definitions are parsed from.

Reading json['title'] yields dynamic, so passing it to a String? parameter is an implicit downcast: the analyzer accepts it and the program throws at the point of use, often several frames from the malformed field. These accessors move that failure to the boundary and name the field, and they let the package build with strict-casts enabled.

final json = JsonReader(element, context: 'QuestionStep');
QuestionStep(
  title: json.string('title'),
  count: json.integer('count') ?? 4,
  isOptional: json.boolean('isOptional'),
);

Every accessor tolerates a null map and a missing key, returning null. A value of the wrong type is an error rather than a silent null: a definition that says "count": "many" is a mistake worth surfacing.

Constructors

JsonReader(Map<String, dynamic>? source, {String context = 'element'})
Creates a reader over source.
const

Properties

context String
Name used in error messages to identify what was being parsed.
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
source Map<String, dynamic>?
The map being read, which may be null.
final

Methods

boolean(String key) bool?
The boolean at key, or null. Accepts "true" and "false".
decimal(String key) double?
The double at key, or null. Accepts a numeric string.
enumValue<T extends Enum>(String key, List<T> values) → T?
The enum constant at key whose name matches, or null.
has(String key) bool
Whether key is present with a non-null value.
integer(String key) int?
The integer at key, or null. Accepts a numeric string.
list(String key) List
The list at key, or an empty list when absent.
map(String key) Map<String, dynamic>?
The nested object at key, or null.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
number(String key) num?
The number at key, or null. Accepts a numeric string.
raw(String key) Object?
The raw value at key, untyped.
string(String key) String?
The string at key, or null.
toString() String
A string representation of this object.
inherited

Operators

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