deleteDoc method
Deletes a document both locally and from Firestore.
Performs an optimistic delete by updating the local state first, then syncing with Firestore. If the remote delete fails, the local state remains updated.
Parameters:
id
- The document IDdoNotifyListeners
- Whether to notify listeners of the changetransaction
- Optional transaction for atomic operations
Returns a TurboResponse
indicating success or failure
Implementation
@protected
Future<TurboResponse> deleteDoc({
required String id,
bool doNotifyListeners = true,
Transaction? transaction,
}) async {
try {
log.debug('Deleting doc with id: $id');
final doc = _doc.value;
if (doc == null) {
throw StateError('Document not found');
}
deleteLocalDoc(
id: id,
doNotifyListeners: doNotifyListeners,
);
final future = api.deleteDoc(
id: id,
transaction: transaction,
);
final turboResponse = await future;
if (transaction != null) {
turboResponse.throwWhenFail();
}
return turboResponse;
} catch (error, stackTrace) {
if (transaction != null) rethrow;
log.error(
'$error caught while deleting doc',
error: error,
stackTrace: stackTrace,
);
return TurboResponse.fail(error: error);
}
}