get method

Future<Map<String, Object?>?> get(
  1. String path
)

Reads path inside the transaction.

Firestore requires every read to happen before any write, and rejects a transaction that does otherwise. Enforced here so the answer is a clear error at the call site rather than a rejection at commit.

Implementation

Future<Map<String, Object?>?> get(String path) {
  if (_wrote) {
    throw StateError(
      'a transaction must do all of its reads before any of its writes; '
      'get("$path") came after a write',
    );
  }
  final completer = Completer<Map<String, Object?>?>();
  final receive = RawReceivePort();
  receive.handler = (Object? message) {
    receive.close();
    final bytes = message! as Uint8List;
    final seq = ByteData.sublistView(bytes).getInt64(8, Endian.host);
    if (seq < 0) {
      completer.completeError(
        FirestoreException(
          seq.toInt(),
          'read failed',
          'transaction get $path',
        ),
      );
      return;
    }
    completer.complete(decodeDocument(bytes));
  };
  final p = path.toNativeUtf8();
  final rc = fdbFsTxnGet(_id, p.cast(), receive.sendPort.nativePort);
  calloc.free(p);
  if (rc != 0) {
    receive.close();
    return Future.error(StateError('transaction get $path failed ($rc)'));
  }
  return completer.future;
}