findUnique method Null safety

T? findUnique()

Finds the only object matching the query. Returns null if there are no results or throws if there are multiple objects matching.

Note: offset and limit are respected, if set. Because limit affects the number of matched objects, make sure you leave it at zero or set it higher than one, otherwise the check for non-unique result won't work.


T? findUnique() {
  T? result;
  Object? error;
  final visitor = dataVisitor((Pointer<Uint8> data, int size) {
    if (result == null) {
      try {
        result = _entity.objectFromFB(
            _store, InternalStoreAccess.reader(_store).access(data, size));
        return true;
      } catch (e) {
        error = e;
        return false;
    } else {
      error = UniqueViolationException(
          'Query findUnique() matched more than one object');
      return false;
  checkObx(C.query_visit(_ptr, visitor, nullptr));
  if (error != null) throw error!;
  return result;