Firebase 删除云Firestore中所有用户使用但仅一个用户使用的数据

Firebase 删除云Firestore中所有用户使用但仅一个用户使用的数据,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我试图构建我的数据,这样当有人向数据库添加新产品时,所有用户都会收到他们列表中的新产品,因此新产品会出现在他们的屏幕上。 但如果用户决定删除该产品,他们可以删除它,但只能为自己删除。我是否应该使用云Firestore排序/排序/过滤来完成这项工作 我想我可以用下面这样的东西: final theProduct = Provider.of<Products>(context); products = await firestore.collection('products').get

我试图构建我的数据,这样当有人向数据库添加新产品时,所有用户都会收到他们列表中的新产品,因此新产品会出现在他们的屏幕上。 但如果用户决定删除该产品,他们可以删除它,但只能为自己删除。我是否应该使用云Firestore排序/排序/过滤来完成这项工作

我想我可以用下面这样的东西:

final theProduct = Provider.of<Products>(context);

products = await firestore.collection('products').getDocuments()

for (product in products.documents) {
await firestore.collection('products').add({
'nameOfProduct': theProduct.nameOfProduct
});
}
final theProduct=Provider.of(上下文);
products=Wait firestore.collection('products').getDocuments()
用于(产品中的产品。文档){
等待firestore.collection(“产品”)。添加({
“产品名称”:产品的名称
});
}
但是我得到一个文档属性错误

也许我需要再收集一次,然后再试试?但是我还需要能够使用登录用户的userID检索回数据

如有任何提示/有用的链接,将不胜感激



所以我想我要做的是迭代一个集合中的所有文档,然后是document().collection(“”).add()一些东西到每个集合中。希望这能让我对我的问题有更多的了解。

问题:用户决定删除该产品,他们可以删除,但只能为自己删除

答:您可以在“用户配置文件”部分创建列表,您可以在其中拥有所有产品文档id,您可以使用该文档id获取特定的产品详细信息&当用户单击“删除”按钮时,您可以从该集合中删除该特定文档id

我不确定你在那里用示例代码做什么

如果要向数据库添加数据,请使用:

await Firestore().collection('Products').add({
  'nameOfProduct': theProduct.nameOfProduct,
});
这将为您的产品生成文档随机id

要从特定id获取数据,请使用:

DocumentSnapshot documentSnapshot = await Firestore().collection('Products').document('document').get();
然后,您可以使用该文档快照来获取特定密钥的数据,例如

documentSnapshot.data['Product Name']

感谢那些评论的人。我发现实现这一点的方法是使用每个产品包含的唯一值,并在Firestore中按用户将其添加到列表中。然后从Firestore获取该列表并过滤掉相应的产品。无需删除。

您可以尝试将数据保存在两个集合
mydata
public
中,并且用户仅为
mydata
集合删除其数据,并将其保留在公共位置,每个人都可以看到它