confirmAndDeletePlace function
- required MarkerData marker,
- required BuildContext context,
- required List<
Place> allPlaces, - required void setState(
- void ()
Confirms and deletes a place with optimistic UI updates.
Implementation
Future<void> confirmAndDeletePlace({
required MarkerData marker,
required BuildContext context,
required List<Place> allPlaces,
required void Function(void Function()) setState,
}) async {
final confirmed = await showDeleteConfirmationDialog(context, marker);
if (!confirmed || !context.mounted) return;
final prep = prepareDeletePlace(marker: marker, allPlaces: allPlaces);
if (!prep.success) {
if (context.mounted) showPlaceNotFoundSnackbar(context);
return;
}
setState(() => allPlaces.removeAt(prep.removedIndex!));
if (!context.mounted) return;
// Update cache BEFORE server delete to prevent race condition
// (placesChangeNotifier triggers _loadAllPlaces which would restore old data)
updateCacheAfterDelete(allPlaces);
showDeletingSnackbar(context);
final success = await performDeleteOnServer(
placeId: marker.id,
context: context,
isEncrypted: marker.isEncrypted,
);
if (!context.mounted) return;
if (success) {
showDeleteSuccessSnackbar(context);
} else {
setState(
() => restorePlace(
allPlaces: allPlaces,
originalIndex: prep.removedIndex!,
removedPlace: prep.removedPlace!,
),
);
showDeleteErrorSnackbar(context);
}
}