I am using Isar to save some local data for my Flutter app. The data get saved from the app itself and from a background process (using android_alarm_manager_plus plugin).
In the background process I have something like:
@pragma('vm:entry-point') // Mandatory if the App is obfuscated or using Flutter 3.1+
Future<void> callbackDispatcher(int key) async {
debugPrint('started WM');
final dir = await getApplicationDocumentsDirectory();
Isar? isar = Isar.getInstance(Isar.defaultName) ?? await Isar.open([TaskCollectionSchema], directory: dir.path);
IsarCollection<TaskCollection> tasks = isar.taskCollections;
TaskCollection? taskCollection = tasks.getSync(key);
.....
if (nextSchedule != null) {
debugPrint('Updating task schedule time: $nextSchedule');
task.scheduleTimestamp = nextSchedule.millisecondsSinceEpoch;
isar.writeTxnSync(() {
tasks.putSync(TaskCollection.fromTask(task));
});
}
.....
}
Somewhere else in the app I have:
@override
Future<void> init() async {
final dir = await getApplicationDocumentsDirectory();
_isar = await Isar.open([TaskCollectionSchema], directory: dir.path);
_tasks = _isar.taskCollections;
Stream<void> taskChanged = _tasks.watchLazy(fireImmediately: true);
taskChanged.listen(() {
debugPrint('A Task has changed');
_refreshSink();
} as void Function(void event)?);
}
void _refreshSink() {
if (_controller!.isClosed) {
_controller = StreamController<List<Task>>();
}
_controller!.sink.add(getTasks());
}
Everything works as expected apart from the fact that the watchLazy on the collection is not "listening" when the collection get changed by the background process. How can I trigger the listener from the background activity?