singleOrNull method

Future<R?> singleOrNull({
  1. ExecutionOptions options = const ExecutionOptions(),
})

Returns the only selected row, or null when empty.

Throws QUERY.CARDINALITY for more than one row, fetching at most two.

Implementation

Future<R?> singleOrNull({
  ExecutionOptions options = const ExecutionOptions(),
}) async {
  final rows = await take(
    queryState.limit == null || queryState.limit! > 2 ? 2 : queryState.limit!,
  ).get(options: options);
  if (rows.length > 1) {
    throw OrmException(
      'QUERY.CARDINALITY',
      'Expected at most one row, received ${rows.length}.',
    );
  }
  return rows.firstOrNull;
}