getJsonListOrNull<T> function Null safety

List<T>? getJsonListOrNull<T>(
  1. Map<String, dynamic> json,
  2. String key
)

Implementation

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

    T mapper(it) {
      if (it is T) {
        return it;
      } else {
        throw SchemeConsistencyException(
          'Wrong type by key "$key", expected: "List<$T>" '
          'but has got element in list of type: "${it.runtimeType}"',
        );
      }
    }

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