1

I tried with different ways but i can't edit the structure of code

//First way QuerySnapshot querySnapshot = await db.firestoreInstance.collection('user-history').get(); var list = querySnapshot.docs; print('MY LIST ===== $list'); //Second way final CollectionReference collectionRef = db.firestoreInstance .collection(historyCollection); print('MY SECOND LIST ===== $list'); collectionRef.get().then((qs) { qs.docs.forEach((element) { print('MY doc id ${element.id}'); }); }); 

In my firebase collection(historyCollection) i have four documents but the debugger returns me empty array []. Is there another way to call all documents in certain collection through flutter? I'm trying to call this method through FutureBuilder component.

My version of firestore is: "cloud_firestore: ^0.16.0+1"

7
  • When we use the await we dont have to use then , so in the first way remove then , QuerySnapshot querySnapshot = await db.firestoreInstance.collection(historyCollection).get(); and make sure that write the name of colelction correclty Commented May 7, 2021 at 12:53
  • @Anna I edited my post and tried to do that told me, but still nothing. Commented May 7, 2021 at 13:06
  • what do you get in this print('MY LIST ===== $list'); ?? Commented May 7, 2021 at 13:06
  • try to print the length of your list Commented May 7, 2021 at 13:08
  • I/flutter ( 7767): MY LIST ===== [] I/flutter ( 7767): MY LIST LENGTH ===== 0 I/flutter ( 7767): MY SECOND LIST ===== [] I/flutter ( 7767): MY SECOND LIST LENGTH ===== 0 Commented May 7, 2021 at 13:37

2 Answers 2

2

This should do the trick:

Future<List<dynamic>> getCollection(CollectionReference collection) async { try { QuerySnapshot snapshot = await collection.get(); List<dynamic> result = snapshot.docs.map((doc) => doc.data()).toList(); return result; } catch (error) { print(error); return null; } } 
Sign up to request clarification or add additional context in comments.

7 Comments

Doesn't works for me, it's just method with the same code.
Sorry, there was an error in the code. Try it again, it should work now. You just have to pass the reference to the collection that you want to retrieve the documents from.
The first kind of code is correct "recipes.docs..". The problem is that everytime i'm getting empty array []. I don't know what is the problem. I reconsidering this code again and again but i don't see a mistake.
That's weird, are you referencing the correct collection? Also, could you share your firestore rules? That might be the source of the issue.
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if true; } } } It's only for testing purposes, that's why i'm allowing to read and write everyone
|
1

The entire problem was not from these fragments of code. This problem is came out from this that my collections have subcollections. I read about this and i understand that subcollections can live without their ancestors and the only way to access parents is to do this is directly specify the exact path and name of the document. To work this code in my case was needed to add dummy components of my entire set of collections. For more information please look up these two topics:

  1. -> https://firebase.google.com/docs/firestore/using-console
  2. -> Firestore DB - documents shown in italics

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.