0
votes
  1. I have a document with the totalUnpaid amount of a user.
  2. I'm listening to this document with a StreamBuilder.
  3. The stream is a function that returns Stream.

When I register a new user, I create the totalUnpaid document with a field of int 0. Then immediately after, I navigate to a home page that shows the current totalUnpaid amount. Previously, I've hard-coded the current user as a string. Now that I've implemented Firebase authentication, I want the home page to show the totalUnpaid of the new user.

Previously, my stream was this:

  static Stream<DocumentSnapshot> getTotalUnpaid(String currentUser) {
    final doc = Firestore.instance.collection(currentUser).document('totalUnpaid');
    return doc.snapshots();
  }

Now I've created this method to get my current user:

  static Future<FirebaseUser> getCurrentUser() async {
    final FirebaseUser user = await FirebaseAuth.instance.currentUser();
    return user;
  }

How can I consume this Future inside my stream? I will have to use an await, which will require the function to be async which will require the Stream to become a future which will require my StreamBuilder to become a FutureBuilder.

This won't be ideal, since the totalUnpaid needs to update every time the user adds an item.

1

1 Answers

1
votes

You can do the following:

  static Stream<DocumentSnapshot> getTotalUnpaid() async*{
    final FirebaseUser user = await FirebaseAuth.instance.currentUser();
    final doc = Firestore.instance.collection(user.uid).document('totalUnpaid');
    yield* doc.snapshots();
  }

When you mark a function as async* then you can use yield* which will return the values from the Stream.

The above will work assuming you are using the uid as a collection name.