Bernhard
Bernhard

Reputation: 289

flutter update all entries that have a specific item - firebase-database

How do I update many database entries at the same time that have a specific entry

In this case, all "posts" with the entry "8th St" at "street" should be updated, the item "checked" should be set to "true".

db structure:

+ 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
        ...

Something like:

 final FirebaseDatabase _database = FirebaseDatabase.instance;
_database.ref().child("posts").orderByChild("street").equalTo("8th St").update({"checked": true}); 

thanks

Upvotes: 0

Views: 354

Answers (2)

Xuuan Thuc
Xuuan Thuc

Reputation: 2521

Try this:

  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});
            }
          });
        }
);

Upvotes: 1

Xuuan Thuc
Xuuan Thuc

Reputation: 2521

Try this:

 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});
                }
              });
            }
    );

Upvotes: 1

Related Questions