requireMapList function

List<CrossmintJsonMap> requireMapList(
  1. CrossmintJsonMap map,
  2. String key, {
  3. required String entity,
})

Returns the list of nested JSON objects at key in map, or throws a typed CrossmintDeserializationException naming entity and key.

Strict by design: if any element of the list is not a JSON object, this throws rather than silently dropping it. Callers that genuinely want to skip non-object entries should filter the raw list themselves.

Implementation

List<CrossmintJsonMap> requireMapList(
  CrossmintJsonMap map,
  String key, {
  required String entity,
}) {
  final Object? value = map[key];
  if (value is! List) {
    throw CrossmintDeserializationException(
      'Expected JSON array for "$key" while parsing $entity.',
      entity: entity,
      field: key,
      cause: map,
    );
  }
  final List<CrossmintJsonMap> result = <CrossmintJsonMap>[];
  for (int index = 0; index < value.length; index += 1) {
    final Object? element = value[index];
    if (element is! Map<String, Object?>) {
      throw CrossmintDeserializationException(
        'Expected JSON object at "$key[$index]" while parsing $entity.',
        entity: entity,
        field: '$key[$index]',
        cause: map,
      );
    }
    result.add(element);
  }
  return List<CrossmintJsonMap>.unmodifiable(result);
}