onChange method
Create a Stream of changes to any of the specified tables.
Example to get the same effect as watch:
var subscription = db.onChange({'mytable'}).asyncMap((event) async {
var data = await db.getAll('SELECT * FROM mytable');
return data;
}).listen((data) {
// Do something with the data here
});
This is preferred over watch when multiple queries need to be performed together when data is changed.
While the stream can efficiently handle backpressure (paused subscriptions
will not be notified multiple times after resuming, they'll receive a
single aggregated event instead), it also installs a throttle of 30 during
which the stream will be paused automatically. To disable this behavior,
pass null for throttle or use onChangeUnthrottled explicitly.
Implementation
Stream<UpdateNotification> onChange(Iterable<String>? tables,
{Duration? throttle = const Duration(milliseconds: 30),
bool triggerImmediately = true}) {
final filteredStream = tables != null
? updates.transform(UpdateNotification.filterTablesTransformer(tables))
: updates;
final throttledStream = UpdateNotification.throttleStream(
filteredStream, throttle,
addOne: triggerImmediately ? UpdateNotification.empty() : null);
return throttledStream;
}