handleOptimisticSave method
- Place place
Handle optimistic save of place.
Implementation
Future<void> handleOptimisticSave(Place place) async {
// Prevent places change notifications during save.
skipPlacesChangeNotification = true;
// Show immediately (optimistic update)
setState(() {
savingPlaceIds.add(place.id);
allPlaces = [...allPlaces, place];
});
try {
final success = await PlacesService.addPlace(place, context, widget);
if (mounted) {
if (success) {
SnackBarHelper.showSuccess(context, 'Place saved successfully');
} else {
// Rollback on failure.
setState(() {
allPlaces = allPlaces.where((p) => p.id != place.id).toList();
});
SnackBarHelper.showError(context, 'Failed to save place');
}
}
} catch (e) {
// Rollback on error.
if (mounted) {
setState(() {
allPlaces = allPlaces.where((p) => p.id != place.id).toList();
});
SnackBarHelper.showError(context, 'Failed to save place: $e');
}
} finally {
if (mounted) {
setState(() {
savingPlaceIds.remove(place.id);
});
}
skipPlacesChangeNotification = false;
}
}