Search code examples
flutterfirebasedartgoogle-cloud-firestore

Flutter Firestore: How to get Data from specific/acutal Field


i want to get the Field "child" from the collection "Kinder". But only the actually one, not the whole collection.

StreamBuilder<QuerySnapshot>(
        stream: FirebaseFirestore.instance
            .collection("Users")
            .doc(currentUser?.email)
            .collection("Kinder")
            .doc(widget.docID)
   
            
            
            .snapshots(),
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (snapshot.hasError) {
            return Text('Something went wrong');
          }

          if (snapshot.connectionState == ConnectionState.waiting) {
            return Text("Loading");
          }

          return ListView(
            children: snapshot.data!.docs.map((DocumentSnapshot document) {
              Map<String, dynamic> data = document.data()! as Map<String, dynamic>;
              return ListTile(
                title: Text(data['child']),
              );
            }).toList(),
          );
        },
      ),

Thanks!

With this Code i get the right collection but all "child" fields. When i Add ".doc(widget.docID)" i need to add another ".collection(...)"?


Solution

  • Since you call DocumentReference.snapshots(), the return type is a Stream<DocumentSnapshot> (and not a Stream<QuerySnapshot>).

    I recommend keeping the reference docs handy for this type of problem going forward.