createDoc method

  1. @protected
Future<TurboResponse<T>> createDoc({
  1. Transaction? transaction,
  2. required CreateDocDef<T> doc,
  3. bool doNotifyListeners = true,
})

Creates a new document both locally and in Firestore.

Performs an optimistic create by updating the local state first, then syncing with Firestore. If the remote create fails, the local state remains updated.

Parameters:

  • transaction - Optional transaction for atomic operations
  • id - The ID of the document to create
  • doc - The definition of how to create the document
  • doNotifyListeners - Whether to notify listeners of the change

Returns a TurboResponse with the created document reference

Implementation

@protected
Future<TurboResponse<T>> createDoc({
  Transaction? transaction,
  required CreateDocDef<T> doc,
  bool doNotifyListeners = true,
}) async {
  try {
    final pDoc = createLocalDoc(
      doc: doc,
      doNotifyListeners: doNotifyListeners,
    );
    log.debug('Creating doc with id: ${pDoc.id}');
    final future = api.createDoc(
      writeable: pDoc,
      id: pDoc.id,
      transaction: transaction,
    );
    final turboResponse = await future;
    if (transaction != null) {
      turboResponse.throwWhenFail();
    }
    return turboResponse.mapSuccess((_) => pDoc);
  } catch (error, stackTrace) {
    if (transaction != null) rethrow;
    log.error(
      '$error caught while creating doc',
      error: error,
      stackTrace: stackTrace,
    );
    return TurboResponse.fail(error: error);
  }
}