如果满足条件,则从Flutter删除文档



我试图添加一个函数,以便每当按下按钮时,它将使用我的变量并查看数据库中的所有文档。然后,当它找到与变量匹配的文档时,它将删除该项。然而,我一直得到一个错误消息,我需要一个")"在(doc)之后。有没有更好的方法来运行所有的项目来删除它们,或者我做了一些错误的快照/forEach语句?

Object deleteUser() {
// Call the user's CollectionReference to add a new user
if(name!="" && type!="" && location!="") {
items.snapshots().forEach(
(doc) => if(doc.data['name']==deleteName && doc.data['type']==deleteType && doc.data['location']==deleteLocation => doc.delete();));
return items;
}else{
return "There was a null error";
}
}

您的forEach循环是抛出错误的原因,因为它不是有效的dart。我把它清理干净了,这应该对你有用。

Object deleteUser() {
// Call the user's CollectionReference to add a new user
if (name != "" && type != "" && location != "") {
for (var doc in items.snapshots()) {
if (doc.data['name'] == deleteName &&
doc.data['type'] == deleteType &&
doc.data['location'] == deleteLocation) {
doc.delete();
}
}
return items;
} else {
return "There was a null error";
}
}

最新更新