如何同时更新具有特定条目的多个数据库条目
在本例中,所有的"post "加上"第八St"在"street"应更新,项目"检查";应该设置为"true">
db结构:
+ posts
+ MPgslk7gshfasdjg
userName: Luke
street: 8th St
checked: false
...
+ sfdsflk7ghfdgrwj
userName: Joda
street: 1th St
checked: false
...
+ herjztwefhgrdhhr
userName: Vader
street: 8th St
checked: false
...
类似:
final FirebaseDatabase _database = FirebaseDatabase.instance;
_database.ref().child("posts").orderByChild("street").equalTo("8th St").update({"checked": true});
感谢试试这个:
final _database = FirebaseDatabase.instance.reference();
await _database.child("posts").orderByChild("street").equalTo("8th St").get().then(
(value) {
final data = Map<String, dynamic>.from(value.value);
data.forEach((key, value) {
if(value['street'] == "8th St"){
_database.child("posts/$key").update({"checked" : true});
}
});
}
);
试试这个:
final _database = FirebaseDatabase.instance.reference();
await _database.child("posts").get().then(
(value) {
final data = Map<String, dynamic>.from(value.value);
data.forEach((key, value) {
if(value['street'] == "8th St"){
_database.child("posts/$key").update({"checked" : true});
}
});
}
);