setDocument function

Future<void> setDocument(
  1. String path,
  2. Map<String, Object?> data, {
  3. bool merge = false,
})

Writes data to path. With merge, fields absent from data are left alone rather than removed.

Implementation

Future<void> setDocument(
  String path,
  Map<String, Object?> data, {
  bool merge = false,
}) {
  final bytes = Uint8List.fromList(cborEncode(encodeFirestoreValue(data)));
  final p = path.toNativeUtf8();
  final buf = malloc<Uint8>(bytes.length);
  buf.asTypedList(bytes.length).setAll(0, bytes);
  return _awaitOutcome(
    (port) => fdbFsSet(p.cast(), buf.cast(), bytes.length, merge ? 1 : 0, port),
    'set $path',
  ).whenComplete(() {
    // Freed only once the write has been handed to the SDK, which copies it.
    calloc.free(p);
    malloc.free(buf);
  });
}