Decode<T> class

Decodes a model with its fromJson constructor.

The model type comes from build, so Decode(User.fromJson) decodes a User, and a List<User> too, with no list entry of its own. build can take dynamic or Map<String, dynamic>, which json_serializable and freezed generate. A closure needs a typed parameter: Decode((dynamic json) => User.fromJson(json)).

from reads the model from inside the body, for APIs that wrap it, e.g. {"data": {...}}. A dotted key such as 'data.user' reads deeper. For a list, the list itself is read from from.

final Map<Type, dynamic> modelDecoders = decoders([
  Decode(User.fromJson),
  Decode(Order.fromJson, from: 'data'),
]);
Inheritance

Constructors

Decode(T build(Never json), {String? from})

Properties

decodesErrors → bool
Whether this decoder also runs on 4xx/5xx response bodies.
no setterinherited
from → String?
The key the model is read from, or null to use the whole body.
final
hashCode → int
The hash code for this object.
no setterinherited
listDecoder → NyDecoder<List<T>>
Decodes a List<T>, item by item. Items that decode to null are left out.
no setteroverride
listType → Type
The List<T> type this decoder also handles.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
type → Type
The type this decoder builds, used as its key by decoders.
no setterinherited

Methods

decode(dynamic json, NyResponse response, Map<Type, dynamic> decoders) → T?
Decodes json into a T.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

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