transformJsonListOfMapOrNull<T, R> function Null safety

List<T>? transformJsonListOfMapOrNull<T, R>(
  1. Map<String, dynamic> json,
  2. String key,
  3. T transform(
    1. R
    )
)

Implementation

List<T>? transformJsonListOfMapOrNull<T, R>(
  Map<String, dynamic> json,
  String key,
  T Function(R) transform,
) {
  if (json.containsKey(key) && json[key] != null) {
    final List<dynamic> list = getJsonValue(json, key);

    T mapper(it) {
      try {
        return transform(it as R);
      } on Exception catch (e) {
        throw SchemeConsistencyException(
          'Failed to transform value "$it";\ncause: $e',
        );
      }
    }

    return list.isEmpty ? [] : list.map(mapper).toList();
  } else {
    return null;
  }
}