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:
- id - The document ID
doc
- The function to create the documentdoNotifyListeners
- Whether to notify listeners of the changetransaction
- Optional transaction for atomic operations
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);
}
}