removeSavedDestination method
Removes a saved destination for a user
Implementation
Future<void> removeSavedDestination(String uid, String destinationName) async {
try {
// First get current saved destinations
final user = await getUser(uid);
if (user == null) return;
final savedDestinations = user.savedDestinations;
// Find and remove the destination with matching name
final destinationToRemove = savedDestinations.firstWhere(
(destination) => destination['name'] == destinationName,
orElse: () => {},
);
if (destinationToRemove.isNotEmpty) {
if (!_isOnline) {
// Update cache and track change
_updateCacheForArrayOperation(uid, 'savedDestinations', destinationToRemove, isAddition: false);
_trackPendingChanges(uid, {'savedDestinations': FieldValue.arrayRemove([destinationToRemove])});
return;
}
// Online path
await _firestore.collection(_collectionPath).doc(uid).update({
'savedDestinations': FieldValue.arrayRemove([destinationToRemove]),
});
// Update cache
_updateCacheForArrayOperation(uid, 'savedDestinations', destinationToRemove, isAddition: false);
}
} catch (e) {
print('Error removing saved destination: $e');
if (e is FirebaseException && e.code == 'unavailable') {
// We're offline, update cache and track for later
// Note: We would have to load the user earlier to get the destination
rethrow;
} else {
rethrow;
}
}
}