Future<void> getInboxData() async {
String? currentUserId = auth.currentUser?.uid;
if (currentUserId != null) {
try {
// Listen to changes in the "Messages" collection where 'receiverId' is the current user ID
firestore
.collection("Messages")
.where('receiverId', isEqualTo: currentUserId)
.snapshots()
.listen((querySnapshot) {
List<String> differentUsersList = [];
for (var doc in querySnapshot.docs) {
var userInfo = doc.data();
_urlImage = userInfo['imageUrl'];
List<String> idsList = doc['idsList'];
for (String id in idsList) {
if (id == currentUserId) {
return;
}
if (differentUsersList.contains(id)) {
return;
}
differentUsersList.add(id);
var inboxModel = InboxModel(
name: '',
message: doc['message'],
image: '',
backgroundColor: Colors.black,
unReadMessages: 0,
inboxId: '',
uniqueId: doc['uniqueId'],
receiverId: doc['receiverId'],
senderId: doc['senderId'],
timeStamp: doc['timeStamp'],
idsList: doc['idsList'],
);
inboxModelList.add(inboxModel);
// filter krna hai
}
// inboxModelList[].name = userInfo['name'] ?? inboxModelList[].name;
// inboxModelList[].imageUrl = userInfo['imageUrl'] ?? inboxModelList[].imageUrl;
}
getOthersUserData(differentUsersList);
// If the widget is still mounted, update the state
if (mounted) {
setState(() {});
}
});
} catch (error) {
print('Error fetching inbox data: $error');
}
} else {
print('Error: Current user ID is null');
}
}
Comments