skip to Main Content

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

This is the code I tried

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();
                        }
                      }),
                );
              }),
        );
      }
    });
}

This is the code. One streamBuilder to get the users with where Condition. ListViewBuilder to Display users and second streamBuilder to fetch the last message from the chat sub collection.

But I am facing this issue:

2

Answers


  1. Based on the error image, I believe your issue is coming from here:

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

    You are assuming that there will always be a document in the list. You need to consider an empty list scenario.

    Login or Signup to reply.
  2. why can you use combinestream using rxdart

    StreamBuilder(
    stream: CombineLatestStream.list([
      stream0,
      stream1,
    ]),
    builder: (context, snapshot) {
      final data0 = snapshot.data[0];
      final data1 = snapshot.data[1];
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search