首页 > 解决方案 > Flutter 和 Firestore:如何从 add 函数中获取文档 ID?

问题描述

我向 Firestore 添加了一个文档。如何获取我添加的文档的文档 ID?我想将它保存到一个变量中。

顺便提一句。如果这很重要,我将其用于网络应用程序。

FirebaseFirestore.instance.collection('room').add({
    'name': _name,
    'points': 0,
});

标签: firebaseflutterdartwebgoogle-cloud-firestore

解决方案


最后添加.then方法。

使用 docRef 做任何你需要的事情。

FirebaseFirestore.instance.collection('room').add({
    'name': _name,
    'points': 0,
}).then(function(docRef) {
    console.log("Document written with ID: ", docRef.id);
})


推荐阅读