doc method

DocumentReference<T> doc([
  1. String? documentPath
])

Gets a DocumentReference instance that refers to the document at the specified path within this collection.

documentPath a slash-separated relative path to a document. Without it the document gets a new random id of 20 characters, generated on the client (so this works offline).

Returns the DocumentReference instance, with the converter of this collection. Throws an ArgumentError if documentPath is empty or the path it gives does not name a document (doc('SF/landmarks') names a collection).

final CollectionReference<Map<String, Object?>> cities = firestore
    .collection('cities');
final DocumentReference<Map<String, Object?>> sf = cities.doc('SF');

// An id chosen before the write, for example to store it elsewhere too.
final DocumentReference<Map<String, Object?>> created = cities.doc();
await created.set({'name': 'Lisbon', 'id': created.id});

Implementation

DocumentReference<T> doc([String? documentPath]) {
  if (documentPath != null) {
    checkNonEmptyPath('CollectionReference.doc', documentPath);
  }
  final String path = documentPath ?? autoId();
  return DocumentReferenceInternal.forPath<T>(
    query.path.appendField(ResourcePath.fromString(path)),
    firestore,
    converter,
  );
}