flutter - 使用flutter和firestore创建文档ID时如何存储文档ID
问题描述
我正在创建一个 Todo 应用程序,我需要访问存储在 firestore 中的文档的 ID 才能删除它们。但是我不知道在创建它时如何保存它。这是我的添加屏幕中创建新文档的部分。
_firestore.collection('todos').add({
'title' : title,
'type' : type,
'date' : dayDate,
'time' : _dayTime,
'category' : category,
'author' : loggedInUser.uid,
//'id' : doc.id
});
我不知道如何访问它。而且我将我的任务显示在卡片中,当我长按它们时,我希望它们从 Firestore 中删除,但我不能 bc 我没有他们的想法。这是我的空函数:
void deleteTask(){
_firestore.collection("todos").doc().delete();
}
如果您有任何想法,请告诉我。多谢你们!
解决方案
您可以先获取文档 ID,CollectionRef.pots.doc().id
然后再添加它。这样,您将在文档中也拥有 docId,当您拥有文档数据时,您可以访问它。
static createPot(String name, String password) async{
try{
PotModel newPot = PotModel(
name: name,
potId: FirebaseFirestore.instance.collection('pots').doc().id, // will generate a random doc id, which will be used to create the next document
password: password,
ownerId: ViewModelUser.user.value.mobile,
createdAt: Timestamp.now(),
isActive: true,
potActions: [],
users: [ViewModelUser.user.value.mobile]
);
/// creating a new pot
DocumentReference documentReference = await FirebaseFirestore.instance.collection('pots').add(newPot.toJson());
return newPot;
}catch(e){
print(e.toString());
return null;
}
}
推荐阅读
- sql - 在表sql中查找不同用户的记录数
- sql - 如何订购正确的方法以在单个结果集中包含多个组?
- c - C 中用于将 8 字节唯一 ID 缩短为 4 字节的散列函数
- android - 如何在 Android Oreo (8.1) 或更高版本中从 URI 获取文件路径
- openmpi - 不传递数据时的 OpenMPI 发送和接收数据类型
- shell - 在 PhpStorm 中通过命令行打开大量文件
- swift - 在展开可选值时意外发现 nil (firebase, swift)
- go - Fullcalendar 不渲染从 golang gin 获取的事件
- c# - c# 我可以省略这个 `nameof` 这样我就不必将它作为参数传递了吗?
- jinja2 - Django 酥脆形式和 jinja2 2