selectBytes method

Future<Uint8List> selectBytes(
  1. String sql, [
  2. List<Object?> parameters = const []
])

Executes a query and returns the result as JSON-encoded bytes.

final bytes = await db.selectBytes(
  'SELECT id, name FROM users WHERE active = ?',
  [1],
);
// bytes is a Uint8List containing a JSON array, e.g.:
// [{"id":1,"name":"Ada"},{"id":2,"name":"Grace"}]

JSON serialization happens entirely in C — no Dart Map or String objects are created for the result data. The result crosses to Dart as a single Uint8List.

This is ideal for HTTP responses, file export, or any path where the end consumer wants JSON bytes rather than Dart objects.

Note: This method always reads from the reader pool, even inside a transaction. Use select if you need to see uncommitted writes.

Throws a ResqliteQueryException if the SQL is malformed. Throws StateError if called inside a transaction body.

Implementation

Future<Uint8List> selectBytes(
  String sql, [
  List<Object?> parameters = const [],
]) async {
  if (Transaction.current != null) {
    throw StateError(
      'selectBytes() cannot be used inside a transaction. '
      'Use select() instead, which sees uncommitted writes.',
    );
  }

  _ensureOpen();

  final pool = await _readerPool;
  return pool.selectBytes(sql, parameters);
}