jsonClassField<T> function
dynamic
jsonClassField<T>(})
Implementation
dynamic jsonClassField<T> (
dynamic json, List<String> field, T? Function (dynamic) fromJson,
{bool nullable = true, bool skipException = false, T? defaultValue, bool printUnknownException = false}
) {
assert (
(
nullable && ( skipException || !skipException )
)
|| (!nullable && !skipException)
);
T? retval;
try {
dynamic body = jsonField<dynamic> (json, field, nullable: nullable);
if (body != null) {
try {
retval = fromJson (body);
if (retval == null && !nullable) {
throw NoSuchMethodError;
}
} on BodyException {
rethrow;
} on NoSuchMethodError catch (_) {
throw BodyException(
type: BodyExceptionType.isNull,
fieldName: field.join ("_"),
failedType: T,
currentType: retval.runtimeType
);
} catch (error, bt) {
if (printUnknownException) {
Completer().completeError(error, bt);
}
throw BodyException(
type: BodyExceptionType.undefined,
fieldName: field.join ("_"),
);
}
}
} on BodyException {
if (!skipException) {
rethrow;
}
}
return retval ?? defaultValue;
}