问题
I am updating my old flutter code with the new FlutterFire API. I use a setData with merge: true in one of my functions to update some document fields that are saved in a Map. I do change setData to set, but I get an error with the new FlutterFire plugin that "merge" isn't defined. I found in the migration docs that
setData/set now supports SetOptions to merge data/fields (previously this accepted a Map)
The document reference also says:
/// If [SetOptions] are provided, the data will be merged into an existing
/// document instead of overwriting.
Future<void> set(Map<String, dynamic> data, [SetOptions options]) {
assert(data != null);
return _delegate.set(
_CodecUtility.replaceValueWithDelegatesInMap(data), options);
}
In my original code, I am saving new values in a Map<String, dynamic> called changedvalues and then I use setData(changedValues, merge:true)
So, How do I provide SetOptions to make sure merge is true and only the fields in the changedValues are updated in the document?
回答1:
You have to do the following:
set({"name" : "akbarB"}, SetOptions(merge : true))
来源:https://stackoverflow.com/questions/63833697/using-setdata-with-merge-true-in-the-new-flutterfire-api