createDoc method
Future<TurboResponse<T> >
createDoc({
- Transaction? transaction,
- required CreateDocDef<
T> doc, - 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 operationsid
- The ID of the document to createdoc
- The definition of how to create the documentdoNotifyListeners
- 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);
}
}