OneOf<T> class
Decodes a response that can have more than one shape.
Register it in bootstrap/decoders.dart under the shared parent type.
Pick the shape by a field in the body, by status code, by your own
conditions, or with a function:
LoginResult: OneOf<LoginResult>.byKey('status', {
'success': LoginSuccess.fromJson,
'two_factor': TwoFactorRequired.fromJson,
}, otherwise: (json) => LoginFailed(ApiError.fromJson(json))),
A OneOf also decodes 4xx/5xx bodies, and List<T> item by item.
A shape it doesn't recognise leaves the response data null.
Constructors
- OneOf(T? resolve(dynamic json, NyResponse response))
-
Decodes with
resolve, which gets the body and the whole response and returns the value, or null for a shape it doesn't recognise. -
OneOf.byJson(Map<
bool Function(dynamic json, NyResponse response), OneOfBuilder< types, {OneOfBuilder<T> >T> ? otherwise}) -
Picks the shape with your own conditions. Each condition gets the body
and the whole response, and the first that returns true wins, in the
order written.
otherwisebuilds anything else.factory -
OneOf.byKey(String key, Map<
Object?, OneOfBuilder< types, {OneOfBuilder<T> >T> ? otherwise}) -
Picks the shape by the value of
keyin the body.factory -
OneOf.byStatus(Map<
Object, OneOfBuilder< types, {OneOfBuilder<T> >T> ? otherwise}) -
Picks the shape by the response's status code.
factory
Properties
- decodesErrors → bool
-
Error bodies are decoded too, so a 409 or 422 can be one of the shapes.
no setteroverride
- 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 setterinherited - listType → Type
-
The
List<T>type this decoder also handles.no setterinherited - resolve → T? Function(dynamic json, NyResponse response)
-
Returns the value for the body and the response, or null.
final
- 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
jsoninto aT.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