0

When I click on chat tab. It displays red screen error for few seconds. Then it successfully retrieves data. When I click on error. It takes me to second streambuilder.

I/flutter (17397): The getter 'documents' was called on null.

I/flutter (17397): Receiver: null

I/flutter (17397): Tried calling: documents

Widget chatRoomsList(){
    return StreamBuilder(
        stream: chatRooms,
        builder: (context, snapshot) {
          return snapshot.hasData ? ListView.builder(
              itemCount: snapshot.data.documents.length,
              shrinkWrap: true,
              itemBuilder: (context, index) {

                return StreamBuilder(
                    stream: Firestore.instance.collection("chatRoom").document(snapshot.data.documents[index].data["chatRoomId"]).collection("Chats").orderBy("time",descending: true).limit(1).snapshots(),
                    builder: (context, snapshot1) {
                      return snapshot.hasData ? ListView.builder(
                          itemCount: snapshot1.data.documents.length,
                          shrinkWrap: true,
                          itemBuilder: (context, index1) {
                            return  ChatRoomsTile(
                              userName:snapshot.data.documents[index].data['chatRoomId'].toString().replaceAll("_", "").replaceAll(Constants.myName, ""),
                              chatRoomId: snapshot.data.documents[index].data["chatRoomId"].toString(),
                              message: snapshot1.data.documents[index1]["message"].toString(),
                              Time:snapshot1.data.documents[index1]["time"],
                            );
                          }):Container();

                    });
              }):Container();
        });

  }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

1

The issue is inside the second StreamBuilder you have.

In the builder there, you are checking if the snapshot has data:

snapshot.hasData

You should actually be checking for if snapshot1 has data:

snapshot1.hasData

So as the condition returns true always (as snapshot has data at that point), but the actual data being used is from snapshot1, it shows an error, until it actually gets some data in snapshot1 from the stream.

yusufpats
  • 778
  • 4
  • 12