'How can I receive data from firestore 2 collection using 2 streambuilder and Listview.builder?

I have used 2 streambuilder and listviewbuilder. But why this error is shown and I,m confused this code will work properly? Please, anybody have their answer?

I used streambuilder and inside it used ListView.builder. It means, firstly receives data from firestore documents and after check 'if' condition, then again I used another streambuilder, and inside it use Listview. But the Listview children show an error here.

I want to that, when if condition true then firstly receive the data from chats collection enter image description here after that again receive data from another Architecture collection enter image description here

Please help me for solve this problem.

Here is my code

@override
  Widget build(BuildContext context) {
    UserModel _getCurrentUserData = new UserModel.fromMap(currentUserValue);
    return StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('chats')
            .snapshots(),
        builder: (BuildContext,
            AsyncSnapshot<QuerySnapshot<Map<dynamic, dynamic>>> snapshot) {
          if (snapshot.hasData && snapshot.data != null) {
            if (snapshot.data!.docs.isNotEmpty) {
              return ListView.builder(
                  itemCount: snapshot.data!.docs.length,
                  itemBuilder: (BuildContext, int index) {
                    Map<dynamic, dynamic> docData =
                        snapshot.data!.docs[index].data();

                    if (snapshot.hasError)
                      return new Text('Error: ${snapshot.error}');

                    if (docData["senderId"] == _getCurrentUserData.uid ||
                        docData["receiverId"] == _getCurrentUserData.uid) {
                      return docData['senderId'] == _getCurrentUserData.uid
                          ? StreamBuilder(
                              stream: FirebaseFirestore.instance
                                  .collection(docData['receiverDept'])
                                  .doc(docData['receiverDept'])
                                  .collection(docData['receiverIdentity'])
                                  .where('uid',
                                      isEqualTo: docData['receiverId'])
                                  .snapshots(),
                              builder: (context, snapshot) {
                                if (!snapshot.hasData)
                                  return Text("Loading...");
                                return ListView(
                                    children: snapshot.data!.docs <-- [show an Error][4]
                                        .map((Map<dynamic, dynamic> document) {
                                  return ListTile(
                                    onTap: () {
                                      Navigator.push(
                                        context,
                                        MaterialPageRoute(
                                          builder: (context) => chatPage3(
                                              targetUserValue: document,
                                              currentUserValue: currentUserValue),
                                        ),
                                      );
                                    },
                                    title: Row(
                                      children: [
                                        Stack(
                                          alignment: AlignmentDirectional
                                                  .bottomEnd +
                                              AlignmentDirectional(-0.1, -0.3),
                                          children: [
                                            CircleAvatar(
                                              radius: 30,
                                            ),
                                            senderStatus == 'Online'
                                                ? Container(
                                                    width: 12,
                                                    height: 12,
                                                    // alignment: Alignment.bottomLeft,
                                                    decoration: BoxDecoration(
                                                        color: Colors.green,
                                                        shape: BoxShape.circle),
                                                  )
                                                : Container(),
                                          ],
                                        ),
                                        SizedBox(
                                          width: 10,
                                        ),
                                        Column(
                                          mainAxisAlignment:
                                              MainAxisAlignment.center,
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            Text(
                                              senderName,
                                              style: TextStyle(
                                                  fontFamily: 'JosefinSans',
                                                  fontSize: 20,
                                                  fontWeight: FontWeight.w600),
                                            ),
                                            Text(
                                              'Last Text',
                                              style: TextStyle(
                                                fontFamily: 'JosefinSans',
                                                fontSize: 18,
                                                color: Colors.black54,
                                              ),
                                            ),
                                          ],
                                        ),
                                      ],
                                    ),
                                  );
                                }));
                              })
                          : docData["receiverId"] == _getCurrentUserData.uid
                              ? StreamBuilder(
                                  stream: FirebaseFirestore.instance
                                      .collection(docData['senderDept'])
                                      .doc(docData['senderDept'])
                                      .collection(docData['senderIdentity'])
                                      .where('uid',
                                          isEqualTo: docData['senderId'])
                                      .snapshots(),
                                  builder: (context, snapshot) {
                                    if (!snapshot.hasData)
                                      return Text("Loading...");
                                    return ListView(
                                        children: snapshot.data!.docs.map(
                                            (Map<dynamic, dynamic> document) {
                                      return ListTile(
                                        onTap: () {
                                          Navigator.push(
                                            context,
                                            MaterialPageRoute(
                                              builder: (context) => chatPage3(
                                                  targetUserValue: document,
                                                  currentUserValue: currentUserValue),
                                            ),
                                          );
                                        },
                                        title: Row(
                                          children: [
                                            Stack(
                                              alignment: AlignmentDirectional
                                                      .bottomEnd +
                                                  AlignmentDirectional(
                                                      -0.1, -0.3),
                                              children: [
                                                CircleAvatar(
                                                  radius: 30,
                                                ),
                                                senderStatus == 'Online'
                                                    ? Container(
                                                        width: 12,
                                                        height: 12,
                                                        // alignment: Alignment.bottomLeft,
                                                        decoration:
                                                            BoxDecoration(
                                                                color: Colors
                                                                    .green,
                                                                shape: BoxShape
                                                                    .circle),
                                                      )
                                                    : Container(),
                                              ],
                                            ),
                                            SizedBox(
                                              width: 10,
                                            ),
                                            Column(
                                              mainAxisAlignment:
                                                  MainAxisAlignment.center,
                                              crossAxisAlignment:
                                                  CrossAxisAlignment.start,
                                              children: [
                                                Text(
                                                  senderName,
                                                  style: TextStyle(
                                                      fontFamily: 'JosefinSans',
                                                      fontSize: 20,
                                                      fontWeight:
                                                          FontWeight.w600),
                                                ),
                                                Text(
                                                  'Last Text',
                                                  style: TextStyle(
                                                    fontFamily: 'JosefinSans',
                                                    fontSize: 18,
                                                    color: Colors.black54,
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ],
                                        ),
                                      );
                                    }));
                                  })
                              : Container();
                    }
                    return Container();
                  });
            } else {
              return const Center(
                child: Text('Document aren\'t availavle'),
              );
            }
          } else {
            return const Center(
              child: Text('Getting Error'),
            );
          }
        });
  }


Solution 1:[1]

The snapshot object you have is an AsyncSnapshot, which doesn't have a docs property. To get to the docs, you'll want to get the snapshot.data!.docs.

Also see my explanation here of the various types of snapshots that are involved when you load data from Firebase in Flutter: What is the difference between existing types of snapshots in Firebase?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Frank van Puffelen