getDocument function
Reads path, or null when it does not exist.
Implementation
Future<Map<String, Object?>?> getDocument(String path) {
final completer = Completer<Map<String, Object?>?>();
final receive = RawReceivePort();
receive.handler = (Object? message) {
receive.close();
final bytes = message! as Uint8List;
final header = ByteData.sublistView(bytes);
final seq = header.getInt64(8, Endian.host);
if (seq < 0) {
completer.completeError(
FirestoreException(
seq.toInt(),
seq == -2
? 'the document exists but could not be encoded'
: 'read failed',
'get $path',
),
);
return;
}
completer.complete(decodeDocument(bytes));
};
final p = path.toNativeUtf8();
final rc = fdbFsGet(p.cast(), receive.sendPort.nativePort);
calloc.free(p);
if (rc != 0) {
receive.close();
return Future.error(StateError('get $path failed to start ($rc)'));
}
return completer.future;
}