我正在构建一个聊天应用程序,我正在尝试构建一个屏幕,其中包含与其他用户的用户历史对话。因此,基本上我要做的是获取消息字段映射中的所有值(仅取值,而不是键),如下所示:

我想将它与chatRoom中的文档进行比较,如下所示:

如果我比较它们,结果是真的,我想向用户显示该chatRoom中的数据。我真的很难理解如何比较他们提前感谢。
编辑:代码如下:
final String chatId;
Chat({this.chatId});
@override
_ChatState createState() => _ChatState();
}
class _ChatState extends State<Chat> {
// Our own id
final String currentUserId = auth.currentUser?.uid;
// Loading state
bool isLoading = false;
// Query variable
Future<QuerySnapshot> chatHistoryResults;
// All the user rooms will be in this variable
List<String> roomsUserList = [];
// All the chat rooms will be in this variable
List<String> roomsChatList = [];
// All the history chat tickets will be here.
List<QuerySnapshot> tickets = [];
// Getting all the current user rooms in a list of Id rooms.
//TODO: fix the chat history
getRooms() async {
roomsUserList = [];
//roomsChatList = [];
DocumentSnapshot myDocUser = await userRef.doc(currentUserId).get();
Map<dynamic, dynamic> userRoomsMap = await myDocUser.get('messages');
userRoomsMap.forEach((key, value) {
roomsUserList.add(value);
});
print(roomsUserList);
roomsUserList.forEach((element) async {
String chatDoc = chatRef.doc(element).get().toString();
roomsChatList.add(chatDoc.toString());
});
print(roomsChatList);
}
// Build the story tickets here
buildStoryTickets() {
return FutureBuilder(
future: chatRef.get().then((snapshot) {
snapshot.docs.forEach((element) {});
}),
);
}
@override
Widget build(BuildContext context) {
return Provider<AuthService>(
create: (context) => AuthService(),
child: Scaffold(
backgroundColor: Colors.grey[350],
appBar: PreferredSize(
preferredSize: Size.fromHeight(55.0),
child: Header(
title: 'Chat',
),
),
body: ListView(
children: [
//buildStoryTickets(),
Container(
height: 100,
width: 100,
child: FlatButton(onPressed: () {
getRooms(); // Just to check
}),
),
],
),
),
);
}
}我设法将"messages“映射中的所有值插入到一个名为"roomUserList”的列表中
发布于 2021-04-28 04:00:11
首先,您可以使用StreamBuilder从用户和聊天室获取所有数据。一旦你有了消息和聊天室,你就可以通过开发代码逻辑来比较它们。
从firestore获取数据的PFB示例代码:
StreamBuilder<QuerySnapshot>(
stream: _firestore.collection('users').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
//add your comparison logic here
);
}https://stackoverflow.com/questions/67288983
复制相似问题