get<T extends Object?> method

Future<DocumentSnapshot<T>> get<T extends Object?>(
  1. DocumentReference<T> documentRef
)

Reads the document documentRef from the server.

The read never uses the local cache, and the snapshot has no pending writes. A missing document is not an error: the snapshot then has exists false. When documentRef has a converter, the snapshot's data is converted with it.

In a read-write transaction the commit checks that the document has not changed since this read; if it has, the transaction is retried. Every read must come before the first write: after a write the future fails with FirestoreErrorCode.invalidArgument and the transaction does not commit. In a read-only transaction every read sees the same snapshot of the database.

final DocumentSnapshot<Map<String, Object?>> snapshot =
    await transaction.get(firestore.doc('cities/SF'));

The future fails with an ArgumentError if documentRef belongs to another FirebaseFirestore instance, and with a FirestoreException when the server cannot be reached or rejects the read (for example FirestoreErrorCode.permissionDenied).

Implementation

Future<DocumentSnapshot<T>> get<T extends Object?>(
  DocumentReference<T> documentRef,
) async {
  _firestore.validateReference(documentRef);
  final List<MaybeDocument> result = await _transaction.lookup(<DocumentKey>[
    documentRef.key,
  ]);

  if (result.length != 1) {
    throw fail('Mismatch in docs returned from document lookup.');
  }

  final MaybeDocument doc = result.first;
  if (doc is Document) {
    return DocumentSnapshotInternal.fromDocument<T>(
      _firestore,
      doc,
      isFromCache: false,
      hasPendingWrites: false,
      converter: documentRef.converter,
    );
  } else if (doc is NoDocument) {
    return DocumentSnapshotInternal.fromNoDocument<T>(
      _firestore,
      doc.key,
      isFromCache: false,
      hasPendingWrites: false,
      converter: documentRef.converter,
    );
  } else {
    throw fail(
      'BatchGetDocumentsRequest returned unexpected document type: '
      '${doc.runtimeType}',
    );
  }
}