add method

Future<DocumentReference<T>> add(
  1. T data
)

Adds a new document to this collection with the specified data, assigning it a document ID automatically.

data the data of the new document: a Map<String, Object?>, or a T that the converter of this collection turns into one. The future fails with an ArgumentError for a value Firestore cannot store.

Returns a Future that will be resolved with the DocumentReference of the newly created document, once the backend has acknowledged the write. The write is applied to the local cache at once, so listeners see the document immediately; while the client is offline the future does not complete. If you need the reference without waiting, use doc() and DocumentReference.set instead.

final DocumentReference<Map<String, Object?>> ref = await firestore
    .collection('messages')
    .add({'text': 'hello', 'sent': FieldValue.serverTimestamp()});
print('added ${ref.id}');

Implementation

Future<DocumentReference<T>> add(T data) async {
  checkNotNull<Object>(data, 'Provided data must not be null.');
  final DocumentReference<T> ref = doc();
  await ref.setFor('CollectionReference.add', data);
  return ref;
}