native_location_tracker library
Native-first background location tracking for Flutter.
This package provides robust background location tracking with:
- Cross-platform support (Android + iOS)
- Native-first persistence (SQLite on both platforms)
- Automatic batch uploads via native HTTP
- Adaptive sampling based on speed and motion state
- Configurable tracking modes (lowPower vs fast)
Architecture
Native is the single source of truth for persistence and upload. Dart observes native state and provides UI-facing streams only.
Quick Start
import 'package:native_location_tracker/native_location_tracker.dart';
// Initialize with your upload endpoint
await BackgroundLocation.initialize(
config: UploadConfig(
uploadUrl: 'https://api.example.com/location/update',
accessToken: myToken,
),
);
// Start tracking
await BackgroundLocation.instance.startTracking(
TrackingOptions(sessionId: 'trip-123', mode: TrackingMode.fast),
);
// Listen for updates
BackgroundLocation.instance.locationStream.listen((point) {
print('Location: ${point.lat}, ${point.lng}');
});
// Stop when done
await BackgroundLocation.instance.stopTracking();
Platform Setup
See README.md for Android manifest and iOS Info.plist configuration.
Classes
- BackgroundLocation
- Main entry point for background location tracking.
- BackgroundLocationImpl
- Implementation of BackgroundLocationManager using platform channels.
- BackgroundLocationManager
- Abstract interface for background location management.
- EffectiveTrackingParams
- Effective tracking parameters after applying policy.
- LocationPoint
- A single location point captured by the background tracker.
- NativeTrackingState
- Native tracking state emitted by the platform service.
- NativeUploadConfig
- Native-stored upload/auth config snapshot.
- SpeedThresholds
- Speed thresholds for adaptive sampling (in km/h).
- TrackingOptions
- Configuration options for background location tracking.
- TrackingPolicy
- Policy engine for adaptive sampling and tracking parameters.
- TrackingState
- Information about the current tracking state.
- UploadConfig
- Configuration for native-side HTTP upload of location batches.
Enums
- LocationPriority
- Location accuracy priority for native platforms.
- LocationSource
- Source of the location data.
- TrackingMode
- Tracking mode that determines update frequency and battery usage.
- TrackingStatus
- Status of the background location tracking.
Extensions
- TrackingOptionsPolicy on TrackingOptions
- Extension for convenient params access on TrackingOptions.