singleOrNull method

(Entity, A, B)? singleOrNull()

Implementation

(Entity, A, B)? singleOrNull() {
  final driver = Query.chooseDriver(_driverCandidates);
  final driverIsA = identical(driver, _a);
  final driverIsB = identical(driver, _b);
  _world.beginQuery(this);
  try {
    (Entity, A, B)? 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;
      final bDense = driverIsB ? i : _b.denseIndexOf(entityIndex);
      if (bDense < 0) continue;
      if (!Query.passesFilters(entityIndex, _withStores, _withoutStores)) {
        continue;
      }
      if (match != null) {
        throw StateError(
          'Query2<$A, $B>.single: expected exactly one matching entity, '
          'found more than one.',
        );
      }
      match = (
        _world.entities.resolve(entityIndex),
        _a.valueAt(aDense),
        _b.valueAt(bDense),
      );
    }
    return match;
  } finally {
    _world.endQuery();
  }
}