我在处理一个问题时遇到了很多困难。
在 flutter 上使用 cloud_firestore,如何在集合中创建一个新文档(如果它不存在),在该集合中创建一个新文档并在该文档中创建一个具有名称、bool 类型和值的新字段真的?
这就是我现在所拥有的:
await Firestore.instance
.collection(collectionName)
.document(documentName)
.get()
.then((DocumentSnapshot ds) {
// do something here
});
谢谢你。
要创建新文档,您需要使用add()
:
Firestore.instance.collection("users").add(
{
"name" : "peter",
"boolVal" : true
}).then((value){
print(value.documentID);
});
add()
将创建一个带有随机 id 的文档,如果您想使用自己的 id 则使用setData()
:
Firestore.instance.collection("users").document(id).setData(
{
"name" : "peter",
"boolVal" : true
}).then((value){
print(value.documentID);
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句