how to edit an array element present in firestore [duplicate]

杀马特。学长 韩版系。学妹 提交于 2021-02-17 07:03:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!