Darjusch
Darjusch

Reputation: 188

How do i access nested items in firestore?

I am having trouble to access my datafrom the firestore. Adding data works perfectly well. Accessing with an easy example works aswell: /cars/FaTFlIlSHl5qYSdyg0EZ

Firestore.instance
              .collection('cars')
              .snapshots()

           Text(snapshot.data.documents[0]['brandname']),

But when i want to access my nested data with the following structure:

/Stände/Testing/Tue, 7.21.2020/Holstenstraße

 Firestore.instance
              .collection('Stände')
              .snapshots(),
          
           Text(snapshot.data.document('Testing').collection('Tue, 
           7.21.2020').document('Holstenstraße')['Holstenstraße']['erdbeeren'] 
           ['erdbeerenAB']),

I get the error:

Class 'QuerySnapshot' has no instance method 'document'.
Receiver: Instance of 'QuerySnapshot'
Tried calling: document("Testing")

Upvotes: 0

Views: 179

Answers (1)

Darshan Rathod
Darshan Rathod

Reputation: 611

Once you get the QuerySnapshot you can not directly perform another Firebase Query on it.

1). Let's say you want testing collection('Tue, 7.21.2020')'s all Documents.

Firestore.instance.collection('Stände')
        .document('testing')
        .collection('Tue, 7.21.2020')
        .getDocuments().then((ds) {
          List<DocumentSnapshot> list = ds.documents;
    });
  • it will contains 3 document as per your Database

  • Then you can access by

    list.elementAt(index).data['Holstenstraße']['erdbeeren'] ['erdbeerenAB']

2). If you want only one particular document => collection.document.collection.onlyOneDocument (documentID needed)

Firestore.instance.collection('Stände')
        .document('testing')
        .collection('Tue, 7.21.2020')
        .document('Holstenstraße').get().then((DocumentSnapshot ds) {
          print(ds.data['Holstenstraße']['erdbeeren']['erdbeerenAB']);
    });

Upvotes: 2

Related Questions