update method

Future<DocumentReference<Object?>> update({
  1. required T item,
  2. SetOptions? setOptions,
  3. required String type,
  4. DocumentReference<Object?>? parent,
  5. MapperCallback<T>? mapper,
})

Update the document which is corresponding to the given item.

Firestore allows you to update only some fields instead of rewriting the whole document. The mapper function is used map the item to a Map with only the required fields to be updated.

If mapper is null FirebaseRepository.toMap will be used to map the item. In this case, the whole document will be rewritten.

type and parent can be null if the item has a valid DocumentReference as DBModel.ref. If reference is null a new item will be created in the database with FirebaseRepository.add.

Returns the DocumentReference which was used to update or add the item.

Implementation

Future<DocumentReference> update({
  required T item,
  SetOptions? setOptions,
  required String type,
  DocumentReference? parent,
  MapperCallback<T>? mapper,
}) async {
  final data = mapper?.call(item) ?? toMap(item, setOptions);
  if (item.ref == null) {
    return await add(item: item, type: type, parent: parent);
  }
  if (await _checkConnectivity()) {
    await item.ref!.update(data);
  } else {
    item.ref!.update(data);
  }
  return item.ref!;
}