Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion lib/geolocation_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ class GeolocationService {
} else {
LocationCache.set(location);
try {
// Sync current location to server
await bg.BackgroundGeolocation.sync();

// CRITICAL FIX: Destroy the location after successful sync
// Without this, locations accumulate in the plugin's database
// and get resent every time sync() is called
await bg.BackgroundGeolocation.destroyLocation(location.uuid);
} catch (error) {
developer.log('Failed to send location', error: error);
}
Expand All @@ -66,7 +72,26 @@ class GeolocationService {
if (location.extras?.isNotEmpty == true) return false;

final lastLocation = LocationCache.get();
if (lastLocation == null) return false;

// If cache is null (e.g., in headless isolate), check SharedPreferences directly
if (lastLocation == null) {
final lastTimestamp = Preferences.instance.getString(Preferences.lastTimestamp);
if (lastTimestamp != null) {
// Not the first location ever - apply minimum filtering to prevent excessive uploads
final duration = DateTime.parse(location.timestamp).difference(DateTime.parse(lastTimestamp)).inSeconds;
final isHighestAccuracy = Preferences.instance.getString(Preferences.accuracy) == 'highest';

if (!isHighestAccuracy) {
final fastestInterval = Preferences.instance.getInt(Preferences.fastestInterval);
if (fastestInterval != null && duration < fastestInterval) {
developer.log('Location too frequent (${duration}s < ${fastestInterval}s), deleting');
return true;
}
}
}
// First location or sufficient time passed
return false;
}

final isHighestAccuracy = Preferences.instance.getString(Preferences.accuracy) == 'highest';
final duration = DateTime.parse(location.timestamp).difference(DateTime.parse(lastLocation.timestamp)).inSeconds;
Expand Down