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"

vlladislav45
  • 63
  • 2
  • 9

2 Answers2

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;
    }
  }
Momar
  • 149
  • 7
  • Doesn't works for me, it's just method with the same code. – vlladislav45 May 08 '21 at 14:29
  • 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. – Momar May 09 '21 at 15:40
  • 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. – vlladislav45 May 09 '21 at 16:23
  • That's weird, are you referencing the correct collection? Also, could you share your firestore rules? That might be the source of the issue. – Momar May 10 '21 at 01:12
  • ```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 – vlladislav45 May 10 '21 at 06:22
  • I see, there's nothing wrong with the rules either. I really have no idea what's going on – Momar May 10 '21 at 11:44
  • Wait a minute, are you doing this on Flutter Web? Because I remember some versions not working properly on the web for some reason. – Momar May 10 '21 at 11:46
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
vlladislav45
  • 63
  • 2
  • 9