问题
I have been trying to add data to sections in my firestorm database, I have a collection -> document -> data fields. Whenever I use setData({'key': 'value'}) is always overwrites the data already in the document. Is there anyway around this?
回答1:
That is because what setData(object)
is used for
To create or overwrite a single document
While update(object)
To update some fields of a document without overwriting the entire document
So what you need is to use update()
回答2:
Using merge: true
in your setData()
statement prevents overwrite.
This seems similar to the update()
method but you can use it even when document does not exist (ie is being created).
来源:https://stackoverflow.com/questions/49801496/flutter-firestore-overwrites-data-when-i-use-set-data