listen method
Load data while monitoring Firestore for real-time updates.
It will continue to monitor for updates until dispose().
Implementation
Future<void> listen() async {
if (subscriptions.isNotEmpty) {
return;
}
if (_loadCompleter != null) {
return loading;
}
_loadCompleter = Completer<void>();
await FirebaseCore.initialize();
FirebaseCore.enqueueTransaction(() async {
if (subscriptions.isNotEmpty) {
return;
}
if (_loadCompleter == null) {
return;
}
try {
await onLoad();
if (subscriptions.isNotEmpty) {
return;
}
if (_loadCompleter == null) {
return;
}
final streams = references.map(
(reference) => reference.snapshots(),
);
subscriptions.addAll(
streams.map((stream) => stream.listen(_handleOnUpdate)),
);
await Future.wait(streams.map((stream) => stream.first));
await onDidListen();
_loadCompleter?.complete();
_loadCompleter = null;
} catch (e) {
_loadCompleter?.completeError(e);
_loadCompleter = null;
} finally {
_loadCompleter?.complete();
_loadCompleter = null;
}
});
return loading;
}