Dani
Dani

Reputation: 4196

Flutter Firestore: update array of object by param

I have a document with these param:

"rent": "text",
"leases": [
 {
  "id": 1,
  "title": "text",
 },
 {
  "id": 2,
  "title": "text",
 }
]

I add new elements to the array like this:

.update({'leases': FieldValue.arrayUnion([newLease])});

But how can I update that id 2 only?

Upvotes: 2

Views: 7246

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600006

The array operations can either add an item that isn't in the array yet (FieldValue.arrayUnion) or remove an item that is in the array already (FieldValue.arrayRemove ). There is no way to atomically update an existing item, by its index or otherwise.

So that means the only option is to:

  1. Read the document into your application code.
  2. Get the array from it.
  3. Update the array in your application.
  4. Write back the entire array to the database.

Also see:

Upvotes: 12

Related Questions