首页 > 解决方案 > 有没有办法在flutter firebase中查询文档中的整个列表?

问题描述

我正在构建一个具有聊天功能的应用程序,现在我正在尝试显示所有最后的历史票证。我有一个存储所有当前用户房间聊天的用户数据库,我将它提取到 List currentUserRoomList。

// Getting a stream of the chat
  getChatStream() {
    return chatRef
        .doc(currentUserRoomList.first) // << here
        .collection('messageId')
        .orderBy('timeStamp', descending: true)
        .limit(1)
        .snapshots();
  }

  // Build the story tickets here
  buildStoryTickets() {
    return StreamBuilder(
      stream: getChatStream(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return loading();
        }
        List<ChatHistory> tickets = [];
        snapshot.data.docs.forEach((doc) {
          ChatHistory ticket = ChatHistory(
            id: doc.data()['id'],
            otherUserId: doc.data()['otherId'],
            message: doc.data()['message'],
            photoUrl: currentUserId == doc.data()['id']
                ? doc.data()['photos'][1]
                : doc.data()['photos'][0],
            displayName: currentUserId == doc.data()['id']
                ? doc.data()['names'][1]
                : doc.data()['names'][0],
            rid: doc.data()['rid'],
            timeStamp: doc.data()['timeStamp'],
          );
          tickets.add(ticket);
        });
        return ListView(children: tickets);
      },
    );

chatRef 来自另一个页面。->

final chatRef = FirebaseFirestore.instance.collection('chatrooms');

我在 chatRef 中有应用程序中的所有房间 ID。我的问题是 getChatStream(),当我只给它一个值时它工作(例如:.doc(currentUserRoomList.first)),但我正在尝试为 currentUserRoomList 中的所有值构建历史票证,我正在努力解决这个问题. 提前致谢!

标签: listfirebaseflutterchatdocumentation

解决方案


推荐阅读