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,
);
},
),
For this, you've to use FutureBuilder inside the StreamBuilder.