0

enter image description here

i want indivisualscore of all the ids to get printed on the console.

when i search for each indivisual id passed it gives me correct answer. for example

 CollectionReference collectionReference =  FirebaseFirestore.instance.collection('score');
    await collectionReference.doc(widget.uid).collection("indivisualscore").snapshots().listen((event) {
      print(event.docs.length);
    }

here i am passing widget.uid, gives me indivisualscore of that particular id passed.

but i now want to obtain indivisualscore of all the ids, in the score collection. it has to do something with the length of the doc in score collection,but i am not able to get it correctly.

thanks.

Riyazat Durrani
  • 588
  • 1
  • 8
  • 20

1 Answers1

1

If you want to get the ids inside the collection score, then you have to do the following:

CollectionReference collectionReference = FirebaseFirestore.instance.collection('score');
var snapshot = await collectionReference.get();
 snapshot.docs.forEach((result) {
    print(result.id);
  });

Reference the collection score then use get() to obtain the documents inside that collection, then the property docs will return a List<DocumentSnapshot> which you can iterate and get all the ids.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • where did you paste it? – Peter Haddad Nov 18 '20 at 09:50
  • if it is not printing, then its not even entering it.. you have to debug it to check – Peter Haddad Nov 18 '20 at 10:00
  • but for indivisual uid,its printing. i debugged it,and its running your code, but not printing anything. – Riyazat Durrani Nov 18 '20 at 10:03
  • printing print(snapshot.docs); shows empty erray in the console, and printing just snapshort gives Instance of 'QuerySnapshot' in console – Riyazat Durrani Nov 18 '20 at 10:06
  • printing snapshort.size prints 0, how is it even possible? – Riyazat Durrani Nov 18 '20 at 10:07
  • are the document id deleted? – Peter Haddad Nov 18 '20 at 10:30
  • check this https://firebase.google.com/docs/firestore/using-console#non-existent_ancestor_documents it seems you deleted the documents since they are in italics according to your image – Peter Haddad Nov 18 '20 at 10:31
  • have you setup any rules at Firestore console? – Yadu Nov 18 '20 at 10:32
  • @Yadu the documents are deleted, check this https://firebase.google.com/docs/firestore/using-console#non-existent_ancestor_documents *the document at path `/mycoll/mydoc/mysubcoll/mysubdoc` can exist even if the ancestor document `/mycoll/mydoc` does not.* – Peter Haddad Nov 18 '20 at 10:37
  • @Yadu check the documents id here https://stackoverflow.com/questions/64890658/how-to-set-documentid-in-firestore-using-flutter – Peter Haddad Nov 18 '20 at 10:38
  • @PeterHaddad if i am using your code on other collection, its working fine, but on this particular collection that is score, ids get saved in italics, and it doesnt respond to the code, – Riyazat Durrani Nov 18 '20 at 10:40
  • delete the whole score collection, start again, and it will work. All the score collection deleted it until there is no "score" (delete from console) – Peter Haddad Nov 18 '20 at 10:42
  • No, you cannot query for collection that are inside of another document in one go, only multiple **documents** in a single collection can be queried, `individualScore` is collection inside a document (which has been deleted as peter noted), correct me if I am wrong – Yadu Nov 18 '20 at 10:47
  • guys, comment wasnt deleted, i learnt that we cannot only keep the collection in the doc, there has to be something in the doc apart from that collection. for more info,check @K.vijaiarivalagan comment on https://stackoverflow.com/questions/48137582/firestore-db-documents-shown-in-italics – Riyazat Durrani Nov 18 '20 at 11:07
  • 1
    appreciate @PeterHaddad for pointing the error,thankyou so much – Riyazat Durrani Nov 18 '20 at 11:14