Relational Data Fetching Fetch data associated to the current user

This lesson is available for PRO members or as a single course purchase. Sign-in and choose a plan below.
Get Unlimited PRO Access

OR


*Enrollment provides full access to this course (and updates) for life.

Relational Data Fetching

The pattern below is useful for listening to a realtime stream that depends on the current user’s UID. The switchmap extension method from RxDart is an essential tool for combining two streams.

file_type_flutter firestore.dart
class FirestoreService {
  /// Listens to current user's report document in Firestore
  Stream<Report> streamReport() {
    return AuthService().userStream.switchMap((user) {
      if (user != null) {
        var ref = _db.collection('reports').doc(user.uid);
        return ref.snapshots().map((doc) => Report.fromJson(doc.data()!));
      } else {
        return Stream.fromIterable([Report()]);
      }
    });
  }
}

Questions?

Ask questions via GitHub below OR chat on Slack #questions