Chetan
Chetan

Reputation: 391

How to update firebase realtime database based on a value in array of objects in flutter?

I am new to flutter as well as firebase and have structured my data as follows:

  {
      "a": {
        "b" : [ {
          "property1" : value1,
          "property2" : value2
           }, {
          "property1" : value3,
          "property2" : value4
          }]
       }
    }

Firebase database has numbered my array objects as 0,1

As you can see b contains array of objects, how do I create a query if I want to update a value in object which contains property2 value as value2 ? Do I have to modify my database structure?

Upvotes: 1

Views: 609

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317427

You won't be able to do this with a single update. You're going to have to:

  1. Query the children of a/b to find the child that contains value2. The name will be a string with the index of the child you found.
  2. With the name of that found child, perform an update of a/b/{child}.

Upvotes: 3

Related Questions