I want to emit null if there's an error in my stream.
In the simplified example below, the user may not have permission to access the document, in which case null is an appropriate stream event for my application.
Stream<String?> idOrNull() {
return FirebaseFirestore.instance
.doc('collection/doc')
.snapshots()
// ...if there's an error, I want to emit `null`
.map((DocumentSnapshot<Map<String, dynamic>> snap) {
return snap.id;
});
}
Reading the Stream : handleError method documentation, there's this less-than-helpful advice:
If you need to transform an error into a data event, use the more generic Stream.transform to handle the event by writing a data event to the output sink.
Looking into Stream.transform and I'm still non-the-wiser on how to accomplish this task. Advice is greatly appreciated.