singleOrNull method

(Entity, A)? singleOrNull()

Implementation

(Entity, A)? singleOrNull() {
  final driver = Query.chooseDriver(_driverCandidates);
  final driverIsA = identical(driver, _a);
  _world.beginQuery(this);
  try {
    (Entity, A)? match;
    for (var i = 0; i < driver.length; i++) {
      final entityIndex = driver.entityIndexAt(i);
      final aDense = driverIsA ? i : _a.denseIndexOf(entityIndex);
      if (aDense < 0) continue;
      if (!Query.passesFilters(entityIndex, _withStores, _withoutStores)) {
        continue;
      }
      if (match != null) {
        throw StateError(
          'Query1<$A>.single: expected exactly one matching entity, found '
          'more than one.',
        );
      }
      match = (_world.entities.resolve(entityIndex), _a.valueAt(aDense));
    }
    return match;
  } finally {
    _world.endQuery();
  }
}