【问题标题】:How can i get document id in flutter我如何在颤动中获取文档ID
【发布时间】:2021-05-01 10:53:48
【问题描述】:

如何获取“内容”为“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)

【问题讨论】:

  • 请分享您的其余代码。首先将'content' 更改为'contents'。在 firebase 中,您将其作为“内容”使用 s

标签: flutter google-cloud-firestore cloud


【解决方案1】:

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

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

【讨论】:

  • 不客气!请考虑将此标记为您问题的答案。
【解决方案2】:

您的字段名称似乎有错字,屏幕截图中为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()

【讨论】:

    猜你喜欢
    • 2019-12-31
    • 2021-09-30
    • 2021-01-26
    • 2019-09-21
    • 1970-01-01
    • 2018-12-08
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多