How can i use two dependent async tasks in a Stream-Builder?

143 views Asked by At

i want to use a StreamBuilder with two async tasks. The first async stream gets the open friend requests from Firebase/Firestore. For every friend_request item, I need a second async task, which gets the profile information for the friend_request item.

The problem is, that I can't get use the second async task in StreamBuilder, because the builder can't be asnc. My current code returns the list too early, because it can't await.

I would be very happy to receive help.

StreamBuilder<QuerySnapshot>(
  stream: firestoreHandler.streamRequestedFriends(),
  builder: (context, snapshot) {
    if (!snapshot.hasData || snapshot.data.docs.isEmpty)
      return Text('No friend requests');

    List<FriendRequestItem> friendList = List<FriendRequestItem>();
    var friendRequests = snapshot.data.docs;
    for (var friendRequest in friendRequests) {
      String friendId = friendRequest.data()['friendId'];
      // firestoreHandler.getUserById() needs to await
      firestoreHandler
        .getUserById(friendId)
        .then((friendProfile) {
          friendList.add(
            FriendRequestItem(
              photoUrl: friendProfile.data()['profile_picture'],
              email: friendProfile.data()['email'],
              onAccept: () {
                firestoreHandler.acceptFriendRequest(friendId);
              },
              onDelete: () {
                firestoreHandler.removeFriendRequest(friendId);
              },
            ),
          );
        });
    }
    return Column(
      children: friendList,
    );
  },
),
1

There are 1 answers

0
Tirth Patel On BEST ANSWER

For this, you've to use FutureBuilder inside the StreamBuilder.

  StreamBuilder<QuerySnapshot>(
    stream: firestoreHandler.streamRequestedFriends(),
    builder: (context, snapshot) {
      if (!snapshot.hasData || snapshot.data.docs.isEmpty)
        return Text('No friend requests');
      List<FriendRequestItem> friendList = List<FriendRequestItem>();
      var friendRequests = snapshot.data.docs;
      for (var friendRequest in friendRequests) {
        String friendId = friendRequest.data()['friendId'];
        return FutureBuilder(
          future: firestoreHandler.getUserById(friendId),
          builder: (context, snapshotFuture) {
            if (snapshot.connetionState == ConnectionState.done) {
              friendList.add(
                FriendRequestItem(
                  photoUrl: snapshotFuture.data.data()['profile_picture'],
                  email: snapshotFuture.data.data()['email'],
                  onAccept: () {
                    firestoreHandler.acceptFriendRequest(friendId);
                  },
                  onDelete: () {
                    firestoreHandler.removeFriendRequest(friendId);
                  },
                ),
              );
              return Column(
                children: friendList,
              );
            }
            return CircularProgressIndicator();
          },
        );
      }
    },
  );