简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM