【问题标题】:Get the data from sub collection after filtering the field of collection using where clause and provide it to stream builder使用 where 子句过滤集合字段后从子集合中获取数据并将其提供给流构建器
【发布时间】:2023-01-13 20:44:44
【问题描述】:

`Access the chats(sub collection) if user field contains my Username

这是我试过的代码

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 用于从聊天子集合中获取最后一条消息。

但我面临这个问题: (https://i.stack.imgur.com/Yi7hG.png)``

【问题讨论】:

    标签: flutter firebase collections


    【解决方案1】:

    根据错误图片,我相信您的问题出在此处:

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

    您假设列表中始终有一个文档。您需要考虑一个空列表场景。

    【讨论】:

      【解决方案2】:

      为什么你可以使用combinestream使用rxdart

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-16
        • 1970-01-01
        • 2020-05-03
        • 2015-03-22
        • 2018-02-12
        • 2017-11-12
        相关资源
        最近更新 更多