Using setData with merge: true in the new FlutterFire API

≯℡__Kan透↙ 提交于 2020-11-30 00:18:39

问题


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

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