首页 > 解决方案 > 当您拥有该文档的数据时,如何访问 firebase 中的 documentID

问题描述

从下面的代码: data 是文档的数据,我想使用该数据来引用同一文档的 documentID。

  return GestureDetector(
    onTap: () {
      Navigator.push(context, MaterialPageRoute(
        builder: (context) {
          return ProductScreen(
            // sends the id of that particular doc
            productID: data.id,
          );
        },
      ));
    },``` 
I have the data of that particular document, and i want to use that data to reference the id of the document
 

标签: firebasefluttergoogle-cloud-firestore

解决方案


您可以where()在查询中使用方法。

FirebaseFirestore.instance
  .collection('users')
  .where('productID', isEqualTo: "TheProductID")
  .get()
  .then((snapshot) {
    print(snapshot.size)
    var matchedDoc = snapshot.docs
    // run a loop on matchedDoc and access the reference property
  });

此查询将返回 aQuerySnapshot并将包含 productID 字段等于提供的值的所有文档。然后您可以使用每个文档的reference属性QueryDocumentSnapshot来获取它们的DocumentReference.


推荐阅读