Solution 1 :
Try:
var rooms = await FirebaseFirestore.instance
.collection("rooms")
.where("users", arrayContains: userId)
.orderBy("latestMessageTime", descending: true)
.snapshots();
Problem :
I have a Stream which seems to work perfectly fine: Every print statement you see in the stream is getting printed. Also when calling if(snapshot.hasData)
it apparently is true. But still, my stream only returns the following: AsyncSnapshot<List<dynamic>>(ConnectionState.done, [], null, null)
.
What do I need to change in order to be able to access the Data in my retVal variable?
Stream<List>? roomsListStream() {
try {
List<RoomsListModel> retVal = [];
print("userId: $userId");
var rooms = FirebaseFirestore.instance
.collection("rooms")
.where("users", arrayContains: userId)
.orderBy("latestMessageTime", descending: true)
.snapshots();
print("rooms: $rooms");
rooms.forEach((element) {
element.docs.forEach((element) {
print("element: $element");
var room = element.data();
print("room: $room");
var roomId = room["roomId"];
var otherUserId =
room["users"].firstWhere((element) => element != userId);
var lastMessage = room["latestMessage"];
var lastMessageTime = room["latestMessageTime"];
print("otherUserId: $otherUserId");
getOtherUser(otherUserId).then((value) {
print("value: $value");
var avatar = value["photoUrl"];
var name = value["name"];
retVal.add(RoomsListModel(
roomId: roomId,
otherUserId: otherUserId,
avatar: avatar,
name: name,
lastMessage: lastMessage,
lastMessageTime: lastMessageTime));
});
});
});
return Stream.value(retVal);
} catch (e) {
print("Error: $e");
}
}
Comments
Comment posted by dart.dev/tutorials/language/streams#modify-stream-methods
see
Comment posted by cipano
Could you kindly elaborate on that? I don’t really get where and why I would need that?
Comment posted by pskink
why you would need that? add
Comment posted by pskink
check when that print is called, it is called
Comment posted by pskink
i already answered that: return
Comment posted by Tyler2P
Your answer could be improved by adding more information on what the code does and how it helps the OP.