jsonListField<T> function

dynamic jsonListField<T>(
  1. dynamic json,
  2. List<String> field, {
  3. T map(
    1. dynamic
    )?,
  4. bool nullable = true,
  5. bool skipExceptions = false,
  6. List<T>? defaultValue,
  7. bool printUnknownException = false,
})

Implementation

dynamic jsonListField<T> (
  dynamic json, List<String> field,
  {
    T Function (dynamic)? map,
    bool nullable = true,
    bool skipExceptions = false,
    List<T>? defaultValue,
    bool printUnknownException = false
  }
) {
  List<T>? retval;
  try {
    Iterable? list = jsonField<dynamic> (json, field,  nullable: nullable);
    if (list != null) {
      retval = [];
      int i = 0;
      for (dynamic item in list) {
        try {
          if (map != null) {
            try {
              retval.add (
                map (item)
              );
              i++;
            } on BodyException catch (error) {
              throw BodyException(
                type: error.type,
                fieldName: "${field.join ("-")}[${error.fieldName}]",
                currentType: error.currentType,
                failedType: error.failedType,
                index: i
              );
            } catch (error, bt) {
              if (printUnknownException) {
                Completer ().completeError(error, bt);
              }
              throw BodyException(
                type: BodyExceptionType.undefined,
                fieldName: field.join ("-"),
                index: i
              );
            }
          } else {
            try {
              assert (item is T);
              retval.add (item);
            } on AssertionError {
              throw BodyException(
                type: BodyExceptionType.isNotType,
                fieldName: field.join ("-"),
                failedType: T,
                currentType: retval.runtimeType,
                index: i
              );
            }
          }
        } on BodyException {
          if (!skipExceptions) {
            rethrow;
          }
        }
      }
    }
  } on BodyException {
    if (!skipExceptions) {
      rethrow;
    }
  } catch (error, bt) {
    if (printUnknownException) {
      Completer ().completeError(error, bt);
    }

    if (!skipExceptions) {
      throw BodyException(
        type: BodyExceptionType.undefined,
        fieldName: field.join ("-"),
      );
    }
  }

  return retval ?? defaultValue;
}