Flutter 如何从文档快照获取firestore文档ID?

Flutter 如何从文档快照获取firestore文档ID?,flutter,google-cloud-firestore,Flutter,Google Cloud Firestore,这正是我试过的代码。。。 代码中的错误是什么?根据,有一个属性,因此我希望它可以工作: QuerySnapshot querySnapshot = await _collectionRef .limit(1) .orderBy('date')

这正是我试过的代码。。。 代码中的错误是什么?

根据,有一个属性,因此我希望它可以工作:

QuerySnapshot querySnapshot = await _collectionRef   
                                  .limit(1)
                                  .orderBy('date')                                                                    
                                  .getDocuments();
var list = querySnapshot.documents;

querySnapshot.documents.map((document) { 
    print('Document : $document'); // not printing anything.
  });

if(list.length > 0) {
   print('ID : ${list[0].documentID}'); // not working
}
对于DocumentSnapshot,
document.documentID

此外,在字符串插值时使用反勾号

querySnapshot.documents[0].documentID
documentID
已弃用,不应使用。不推荐使用
.id

尝试用替换项替换不推荐使用的成员。

如果没有,它会做什么呢?您的线路正在工作!。但我尝试了不同的方法。我将更新代码。请告诉我问题出在哪里。如果你的问题与第一个问题不同,你应该发表另一篇文章,而不是编辑你现有的文章。对不起。同样的问题。一切都一样。我只是删除了一个变量来存储querySnapshot。现在我已经更新了,这是我的代码。我必须使用
forEach
而不是
map
。不确定列表变量为什么没有打印任何内容。我正在使用完整路径
querySnapshot.documents[0]。documentID
感谢您的帮助..它是
document.id
print('ID : ${list[0].documentID}'); // not working
print(`ID : ${list[0].documentID}`); // working
product.id