stream method
Creates a reactive query that re-emits results when underlying tables change.
db.stream('SELECT * FROM tasks WHERE done = ?', [0]).listen((tasks) {
print('${tasks.length} open tasks');
});
The first emission contains the current results. Subsequent emissions occur after any write that modifies tables this query depends on.
Table dependencies are detected automatically via SQLite's authorizer hook — works with JOINs, subqueries, views, and CTEs without requiring a manual table list.
Streams are deduplicated: multiple calls with the same sql and
parameters share a single underlying query. New listeners on an
existing stream receive the cached result immediately.
Unchanged results are suppressed — if a write touches a watched table but doesn't change this query's output, no emission occurs.
Create streams once and reuse them (e.g., as late final fields in
a State class), rather than creating new streams on every build.
Implementation
Stream<List<Map<String, Object?>>> stream(
String sql, [
List<Object?> parameters = const [],
]) {
_ensureOpen();
return _streamEngine.stream(sql, parameters);
}