Home > Software design >  flutter update all entries that have a specific item - firebase-database
flutter update all entries that have a specific item - firebase-database

Time:05-20

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

CodePudding user response:

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

CodePudding user response:

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