Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 961
Dani Avatar asked Dec 05 '25 04:12

Dani


1 Answers

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:

  • How can I update map data that's in a array in firebase? (Flutter)
  • Flutter: Update specific index in list (Firestore)
  • how to edit an array element present in firestore
like image 94
Frank van Puffelen Avatar answered Dec 07 '25 20:12

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!