getJsonList<T> function Null safety

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

Implementation

List<T> getJsonList<T>(
  Map<String, dynamic> json,
  String key,
) {
  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();
}