问题
I have an array of strings present in firestore. I want to edit the string present at a particular index. How can I do this?
For example, let the array be ["help","me","please"];
How can I change "help" to "don't help" in flutter?
The closest I've gotten to editing the array is adding an element, which I do by FieldValue.arrayunion().
Is it possible to edit an element present at a particular index? Or am I gonna have to fetch the entire array, update it locally, and then upload the entire array?
回答1:
It's not possible with a single update. You have to do exactly as you said: read the document, make changes to the array in memory, write the new array back to the document.
回答2:
Is it possible to edit an element present at a particular index?
This is not possible.
Or am I gonna have to fetch the entire array, update it locally, and then upload the entire array?
That's indeed one option. The other option is to use an arrayUnion() and arrayRemove(), which work if the items in the array are unique.
Also see:
- Flutter: Update specific index in list (Firestore)
- Flutter: How to update an Array within an Array in the firestore
来源:https://stackoverflow.com/questions/61875396/how-to-edit-an-array-element-present-in-firestore