stopLocationUpdates method
//////////// //////////// Stops location updates and releases resources with enhanced error handling
Implementation
// II.F - Cleanup Methods
///////////////
/// Stops location updates and releases resources with enhanced error handling
Future<void> stopLocationUpdates() async {
try {
debugPrint('[LOCATION] Stopping location updates at ${DateTime.now()}');
// Stop standard location updates if active
if (_positionSubscription != null) {
await _positionSubscription!.cancel();
_positionSubscription = null;
debugPrint('[LOCATION] Standard position subscription canceled');
}
// Stop foreground service subscription if active
if (_foregroundLocationSubscription != null) {
await _foregroundLocationSubscription!.cancel();
_foregroundLocationSubscription = null;
debugPrint('[LOCATION] Foreground location subscription canceled');
// Only stop the foreground service if navigation is not active
if (!_isNavigationActive) {
bool stopped = await _foregroundService.stopForegroundTask();
debugPrint('[LOCATION] Foreground service stopped: $stopped');
} else {
debugPrint('[LOCATION] Keeping foreground service active during navigation');
}
}
// Close stream controller
if (_locationStreamController != null) {
await _locationStreamController!.close();
_locationStreamController = null;
debugPrint('[LOCATION] Location stream controller closed');
}
debugPrint('[LOCATION] Location updates stopped successfully');
} on PlatformException catch (e) {
// For stopping, just log the error but don't show to user unless critical
debugPrint('[LOCATION] Platform error stopping location updates: ${e.code} - ${e.message}');
// Only show critical errors to user
if (e.code == 'INTERNAL_ERROR') {
_showErrorMessage('Error stopping location service: ${e.message}');
}
} catch (e) {
debugPrint('[LOCATION] Error stopping location updates: $e');
}
}