且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从flutter中使用Firestore中的唯一文档ID进行查询?

更新时间:2022-12-20 11:49:19

如果您想一次获取所有文档,则可以使用:

If u want to get all the documents at once then u can use :

StreamBuilder<QuerySnapshot>(
  stream: Firestore().collection('Workers').snapshots(),
  builder: (context, snapshot) {
    if (snapshot.data != null) {
      // Here u will get list of document snapshots
      final List<DocumentSnapshot> documents = snapshot.data.documents;
      // now u can access each document by simply specifying its number
      // u can also use list view to display every one of them
      return ListView.builder(
        itemCount: documents.length,
        itemBuilder: (context, int index) => Text(documents[index].data['name']),
      );
    } else {
      // Show loading indicator here
    }
  },
);

如果您想获取特定的文档详细信息(如果您具有文档ID),则可以使用:

If u want to get particular document details (if u have the document id) then u can use :

Future<DocumentSnapshot> _getDocument(String documentName) async {
   return await Firestore().collection('Workers').document(documentName).get();
 }

现在您可以通过名称访问字段了,例如

now u can access fields by there name e.g.

documentSnapshot.data['Name']

我希望这个帮助:)