Punreach Rany
Punreach Rany

Reputation: 3700

Flutter & Firestore : How do I get all of the DocumentID from Firestore?

How do I get all of the DocumentID from Firebase and put them into a list.

Stream <List<String>> getMaterial(BuildContext context) {
    final CollectionReference materialColection =
        Firestore.instance.collection('materials');

   return ???????;
  }

other file

List<String> _material = [-------]

I'm looking forward to hearing from you. Thank you.

Upvotes: 0

Views: 49

Answers (1)

sameer kashyap
sameer kashyap

Reputation: 1166

You can try this,

Future <List<String>> getMaterial(BuildContext context) async {

     List<String> ids =[];

    final CollectionReference materialColection =
        Firestore.instance.collection('materials');

    final result = await materialColection.get();  //getDcouments() for < firebase: ^0.14
      
     result.docs.forEach((doc){
          ids.add(doc.id) //doc.documentId for < firebase: ^0.14
      });


   return ids;
  }

This will return the list of ids of the document each time you call the function.

Alternatively, if you want to listen to the stream, it would be best to Warp it in a StreamBuilder() and extract the document ids within the widget

Upvotes: 2

Related Questions