checkCollection method

int checkCollection(
  1. Object? value, {
  2. required String field,
})

Implementation

int checkCollection(Object? value, {required String field}) {
  _rejectReentrantMetadataAccess();
  if (value is! List && value is! Map) {
    throw FormatException(
      'Invalid $_resource $field: expected a collection.',
    );
  }
  final int length;
  try {
    length = value is List ? value.length : (value as Map).length;
  } catch (_) {
    throw FormatException(
      'Invalid $_resource $field: collection length is unavailable.',
    );
  }
  if (length < 0) {
    throw FormatException(
      'Invalid $_resource $field: collection length is negative.',
    );
  }
  if (length > _budget.maxCollectionItems) {
    _throwLimit(
      field: field,
      kind: 'collection items',
      limit: _budget.maxCollectionItems,
      observedAtLeast: length,
    );
  }
  return length;
}