Miguel Lop
Miguel Lop

Reputation: 31

Flutter. Create a List<String> of the firestore documents and collections

I'm trying to fetch a list of documents (documentID) from Firestore and add it to a List. I have seen some options but since I am a little new in this area, perhaps the obvious is becoming difficult for me. And I don't know exactly where in the code those lines should go, for example in an initState.

This is one of the options that I have chosen, but it only generates instances and not the name of the documents as such.

final QuerySnapshot result =
      await Firestore.instance.collection('submits').getDocuments();
final List<DocumentSnapshot> documents = result.documents;

List<String> myListString = []; // My list I want to create.
myListString.add(documents);    // The way I try to add the doc list to my String list.

Example the data base. I want to get a list of the document ID to a List-String-

enter image description here

And if possible, you could tell me if there is an analogous way to apply it to obtain a List but in the case of two or more collections.

Upvotes: 0

Views: 2034

Answers (1)

Andres Silva
Andres Silva

Reputation: 892

It seems like what you want is a list of the document ids, right?

If so, this should work:

final QuerySnapshot result =
      await Firestore.instance.collection('submits').getDocuments();
final List<DocumentSnapshot> documents = result.documents;

List<String> myListString = []; // My list I want to create.

documents.forEach((snapshot) {
   myListString.add(snapshot.documentID)
});

Upvotes: 2

Related Questions