Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using setData with merge: true in the new FlutterFire API

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?

like image 751
AkbarB Avatar asked Sep 10 '20 16:09

AkbarB


Video Answer


1 Answers

You have to do the following:

set({"name" : "akbarB"}, SetOptions(merge : true))
like image 175
Peter Haddad Avatar answered Dec 04 '22 22:12

Peter Haddad