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 onListen();
if (subscriptions.isNotEmpty) {
return;
}
if (_loadCompleter == null) {
return;
}
final stream = reference.snapshots();
subscriptions.add(stream.listen(_handleOnUpdate));
await stream.first;
await onDidListen();
_loadCompleter?.complete();
_loadCompleter = null;
} catch (e) {
_loadCompleter?.completeError(e);
_loadCompleter = null;
} finally {
_loadCompleter?.complete();
_loadCompleter = null;
}
});
return loading;
}