flutter_offline_background_location 1.0.0
flutter_offline_background_location: ^1.0.0 copied to clipboard
Offline background location tracking plugin. Records location when app is closed or without internet.
flutter_offline_background_location #
Offline background location tracking for Flutter (Android). Records location when the app is in background or closed, without requiring internet.
Features #
- Background tracking: Foreground service keeps recording when the app is closed or in background.
- Offline: Uses GPS/device location only; no network needed.
- Configurable: Interval, accuracy, notification text, retention, and max records.
- Session support: Each start/stop creates a session; filter locations by
sessionId. - DB maintenance: Optional
retentionDaysandmaxRecordsto limit storage.
Installation #
Add to your pubspec.yaml:
dependencies:
flutter_offline_background_location: ^1.0.0
Then run flutter pub get.
For local development from a path (e.g. when the plugin lives in your repo), you can use:
dependencies:
flutter_offline_background_location:
path: ../packages/flutter_background_location
Android permissions #
Your app must request location and (on Android 13+) notification permission before starting tracking. The plugin declares in its manifest:
ACCESS_FINE_LOCATIONACCESS_BACKGROUND_LOCATIONFOREGROUND_SERVICEFOREGROUND_SERVICE_LOCATIONPOST_NOTIFICATIONS
Request them at runtime (e.g. with permission_handler) before calling startTracking().
Usage #
- Configure (once, or when you want to change settings):
import 'package:flutter_offline_background_location/flutter_background_location.dart';
final plugin = FlutterBackgroundLocation.instance;
await plugin.configure(const BackgroundLocationConfig(
intervalMinutes: 1,
accuracy: LocationAccuracy.high,
notificationTitle: 'Location tracking',
notificationBody: 'Recording your position.',
retentionDays: 30, // 0 = unlimited
maxRecords: 10000, // 0 = unlimited
));
- Initialize (creates DB and runs cleanup; call before first start):
await plugin.initialize();
- Start / stop tracking:
await plugin.startTracking();
// ...
await plugin.stopTracking();
- Read locations:
final list = await plugin.getLocations(limit: 100);
final last = await plugin.getLastLocation();
Optional filters: sinceTimestamp, sessionId.
- Optional manual cleanup (retention and max records):
await plugin.runCleanup();
- Clear all stored data (deletes every location record):
await plugin.clearAllLocations();
Configuration #
| Field | Description | Default |
|---|---|---|
intervalMinutes |
How often to record (minutes) | 1 |
accuracy |
LocationAccuracy.high / .medium / .low |
high |
notificationChannelId |
Android notification channel | plugin default |
notificationTitle |
Notification title while tracking | "Location tracking" |
notificationBody |
Notification body text | "Recording your position." |
retentionDays |
Keep only last N days (0 = unlimited) | 0 |
maxRecords |
Max rows; oldest deleted first (0 = unlimited) | 0 |
databasePath |
Custom DB path (null = app support dir) | null |
LocationRecord #
Each record has: id, latitude, longitude, timestamp, and optionally accuracy, altitude, speed, bearing, provider, sessionId.
Example app #
Run the example:
cd example
flutter pub get
flutter run
License #
BSD 3-Clause. See LICENSE file.