addDocumentWithId method

Future<String?> addDocumentWithId(
  1. List<String> paths,
  2. Map<String, dynamic> update
)

Adds a new document into the collection in Firebase.

paths is the list of paths to the document. For example: myCollection, documentId which will be constructed to myCollection/documentId. Last item in paths must be documentId. update is the data which will be added to the document.

Implementation

Future<String?> addDocumentWithId(List<String> paths, Map<String, dynamic> update) async {
  assert(
      paths.length % 2 == 0,
      'paths must be even number since it has to point to the document. If you want'
      'to not include specific documentId, use [addDocument] method.');

  try {
    if (_includeAdditionalFields) _includeAdditionalFieldsIntoMap(update, includeCreatedAt: true);
    final pathToDocument = getPathToDocument(paths);

    await _firebaseFirestore.doc(pathToDocument).set(update);
    _loggingService.log('FirestoreHelper.addDocumentWithId: Path: $pathToDocument, Update: $update');
    return paths.last;
  } catch (e, s) {
    _loggingService.log(
      'FirestoreHelper.addDocumentWithId: Failed. Path: $paths, Update: $update, Exception: ${e.toString()}. StackTrace: $s',
      logType: LogType.error,
    );
    return null;
  }
}