Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 11631

Problem displaying data from subcollection in flutter

$
0
0

In my database, I have a "Czat" collection and a "Wiadomosci" subcollecion. The Czat collection has documents that store users messages in 'Wiadomosci' and fields: idSender, idReceiver. I would like to retrieve all the Chat collection that has the idSender specified. To do this, I created a stream that retrieves the data of documents with specific idSender. Then it tries to get the id of the previously mentioned documents and in the next stream enter them in the doc(). Unfortunately, a problem arises. The forEach loop does not execute. How can I fix this?

    return Scaffold(      body: StreamBuilder(        stream: FirebaseFirestore.instance            .collection('Chat')            .where('idSender', isEqualTo: 'O3EweI2Y3pjSHyUm0HPb')            .snapshots(),        builder: (context, snapshot) {          if (snapshot.hasData) {            snapshot.data?.docs.forEach(              (element) {                Builder(                  builder: (context) {                    Stream<List<Message>> readMessage() =>                        FirebaseFirestore.instance                            .collection('Chat')                            .doc(element.id)                            .collection('Wiadomosci')                            .snapshots()                            .map((snapshots) => snapshots.docs                                .map((doc) => Message.fromJson(doc.data()))                                .toList());                    return StreamBuilder(                      stream: readMessage(),                      builder: (context, snapshot) {                        if (snapshot.hasData) {                          final message = snapshot.data!;                          return ListView(                            children: message.map(buildTest).toList(),                          );                        } else {                          return const Center(                            child: CircularProgressIndicator(),                          );                        }                      },                    );                  },                );              },            );            return Text('');          } else {            return const Center(              child: CircularProgressIndicator(),            );          }        },      ),    );

enter image description here


Viewing all articles
Browse latest Browse all 11631

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>