簡體   English   中英

使用where子句過濾集合字段后從子集合中獲取數據並提供給stream builder

[英]Get the data from sub collection after filtering the field of collection using where clause and provide it to stream builder

如果用戶字段包含我的用戶名,則訪問聊天(子集合)

這是我試過的代碼

Stream<QuerySnapshot> _usersStream = FirebaseFirestore.instance .collection("ChatRoom") .where("users", arrayContains: Constant.myname) .snapshots(); 
@override

Widget ChatMessageList() { return StreamBuilder(   
 //  stream: chatMessageStream,
    stream: _usersStream,
    builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
      if (snapshot.data == null) {
        return Center(
            child: CircularProgressIndicator(
          color: Colors.orange.shade600,
        ));
      } else {
        return Container(
          padding: EdgeInsets.only(top: 15),
          height: MediaQuery.of(context).size.height / 1.2,
          child: ListView.builder(
              itemCount: snapshot.data!.docs.length,
              itemBuilder: (ctx, index) {
                QuerySnapshot<Object?>? snap = snapshot.data; // Snapshot
                List<DocumentSnapshot> items =
                    snap!.docs; // List of Documents
                DocumentSnapshot item = items[index];
                String v = item['chatroomid'];
                return SingleChildScrollView(
                  child: StreamBuilder(
                      stream: FirebaseFirestore.instance
                          .collection("ChatRoom/$v/chats")
                          .orderBy("time", descending: true)
                          .limit(1)
                          .snapshots(),
                      builder: (context, snapshot2) {
                        if (snapshot2.hasData) {
                          QuerySnapshot<Object?>? snap2 =
                              snapshot2.data; // Snapshot
                          List<DocumentSnapshot?>? items2 =
                              snap2?.docs; // List of Documents
                          DocumentSnapshot? item2 = items2?[0];
                          print(item2?['message']);
                          return MessageTile(
                            username: item['chatroomid']
                                .toString()
                                .replaceAll("_", "")
                                .replaceAll(Constant.myname, ""),
                            chatroomid: item['chatroomid'],
                          );
                        } else {
                          return CircularProgressIndicator();
                        }
                      }),
                );
              }),
        );
      }
    });
}

這是代碼。 一個 streamBuilder 來獲取具有 where 條件的用戶。 ListViewBuilder 用於顯示用戶,第二個 streamBuilder 用於從聊天子集合中獲取最后一條消息。

但我面臨這個問題:

根據錯誤圖片,我相信您的問題出在此處:

List<DocumentSnapshot?>? items2 = snap2?.docs; // List of Documents
DocumentSnapshot? item2 = items2?[0];

您假設列表中始終有一個文檔。 您需要考慮一個空列表場景。

為什么你可以通過rxdart使用combinestream

StreamBuilder(
stream: CombineLatestStream.list([
  stream0,
  stream1,
]),
builder: (context, snapshot) {
  final data0 = snapshot.data[0];
  final data1 = snapshot.data[1];
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM