如何获取当前用户发送的所有消息

问题描述 投票:0回答:1

我正在尝试检索当前用户发送给其他用户的所有最后消息并将其显示在列表视图中。我无法做出正确的查询。这是我的 firestore 数据库的结构。我将用两张图片来分享。

这是我当前的查询,不起作用

String currentUser = FirebaseAuth.instance.currentUser!.uid;

// Get the messages where the current user is the sender
QuerySnapshot snapshot = await FirebaseFirestore.instance
.collection('chat_rooms')
.where('senderId', isEqualTo: currentUser)
.orderBy('timestamp', descending: true)
.get();
print("the size of the snapshot is ${snapshot.size}");
flutter dart mobile messaging
1个回答
0
投票

您正在

chat_rooms
集合中搜索,而不是在
messages
集合组中搜索。

QuerySnapshot snapshot = await FirebaseFirestore.instance
  .collectionGroup('messages')
  .where('senderId', isEqualTo: currentUser)
  .orderBy('timestamp', descending: true)
  .get();
© www.soinside.com 2019 - 2024. All rights reserved.