decodeWithDecoders function

dynamic decodeWithDecoders(
  1. dynamic json,
  2. Map<Type, dynamic> decoders, {
  3. required Type type,
  4. NyResponse? response,
})

Runs json through the decoder registered for type in decoders.

Also finds the List<T> decoder that every NyDecoder provides, and the built-in decoders for plain JSON types. Throws a StateError when no decoder exists for type.

Implementation

dynamic decodeWithDecoders(
  dynamic json,
  Map<Type, dynamic> decoders, {
  required Type type,
  NyResponse? response,
}) {
  dynamic decoder = findDecoder(type, decoders);
  if (decoder == null) {
    throw StateError(
      'Your bootstrap/decoders.dart file does not contain a decoder for the following class: ${type.toString()} in modelDecoders',
    );
  }
  if (decoder is NyDecoder) {
    return decoder.decode(
      json,
      response ?? NyResponse(response: null, data: null, rawData: json),
      decoders,
    );
  }
  return decoder(json);
}