我如何在颤动中获取文档ID

How can i get document id in flutter

如何获取 'content' 为 'test2' 的文档 ID? enter image description here 我这样试了。。但是条件不行。

 ref.where(ref.doc('content') == 'test2').get().then((QuerySnapshot snapshot){
      snapshot.docs.forEach((document){
        print(document.id);
      });
 });

如何获取特定文档的文档 ID? (我用的是flutter和cloud firestore)

假设ref 是您的 collection 参考,请使用:

await ref.where('contents', isEqualTo: 'test2').get().then((QuerySnapshot snapshot){
      snapshot.docs.forEach((document){
        print(document.id);
      });
 });

您的字段名称似乎有错别字,在屏幕截图中是 contents,但在您的代码中是 content。所以这会导致空快照。

ref.where(ref.doc('contents') == 'test2').get().then((QuerySnapshot snapshot){
      snapshot.docs.forEach((document){
        print(document.id);
      });
 });

还要确保 ref 的值为 FirebaseFirestore.instance.collection('post')

PS:也可以使用这种语法。 ref.where("contents", isEqualTo: "post2").get()