AppConfig class
Application & lifecycle configuration.
AppConfig groups options that control how the SDK integrates with your app’s
lifecycle: start/stop behavior on terminate and reboot, headless/background
behavior, periodic heartbeats, scheduler windows, foreground notification, and
the Android background-permission rationale dialog.
Use this class via Config.app.
BackgroundGeolocation.ready(Config(
app: AppConfig(
stopOnTerminate: false,
startOnBoot: true,
}
));
What belongs in AppConfig?
- Whether tracking stops on app terminate: see AppConfig.stopOnTerminate.
- Whether tracking starts after device reboot: see AppConfig.startOnBoot.
- Running in headless mode on Android: see AppConfig.enableHeadless.
- Periodic heartbeat callback: see AppConfig.heartbeatInterval.
- Scheduler windows for automatic start/stop: see AppConfig.schedule and AppConfig.scheduleUseAlarmManager.
- Foreground notification options (Android): see AppConfig.notification.
- Background-permission rationale (Android 10+): see AppConfig.backgroundPermissionRationale.
- iOS background preventSuspend switch for heartbeats: see AppConfig.preventSuspend.
Platform notes
iOS
- With AppConfig.stopOnTerminate set to
false, the SDK creates a stationary geofence and iOS will relaunch your app in the background upon exiting that region. - AppConfig.preventSuspend is required for heartbeats and has a battery cost. Use sparingly.
Android
- With AppConfig.enableHeadless set to
true, the native background service continues working even when your Flutter UI process is terminated. Pair this with HttpConfig.url to keep uploads flowing. - The scheduler uses
AlarmManagerby default; control this with AppConfig.scheduleUseAlarmManager.
Examples
Configure once at startup:
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg;
void main() async {
final config = bg.Config(
app: bg.AppConfig(
stopOnTerminate: false,
startOnBoot: true,
enableHeadless: true,
heartbeatInterval: 60,
backgroundPermissionRationale: bg.PermissionRationale(
title: "Allow {applicationName} to access this device's location even when the app is closed or not in use.",
message: "This app collects location data to enable recording your trips to work and calculate distance-travelled.",
positiveAction: 'Change to "{backgroundPermissionOptionLabel}"',
negativeAction: 'Cancel',
),
notification: bg.Notification(
title: 'Background Geolocation',
text: 'Tracking location',
smallIcon: 'mipmap/ic_launcher',
),
schedule: [ '1-5 09:00-17:00' ], // Weekdays 9–5
scheduleUseAlarmManager: true,
),
);
await bg.BackgroundGeolocation.ready(config);
}
Update later at runtime:
await bg.BackgroundGeolocation.setConfig(bg.Config(
app: bg.AppConfig(
heartbeatInterval: 120, // slow down heartbeats
),
));
Migration from flat Config properties
The following legacy properties are deprecated on Config and should now
be set via Config.app: Config.stopOnTerminate, Config.startOnBoot,
Config.enableHeadless, Config.heartbeatInterval, Config.schedule,
Config.scheduleUseAlarmManager, Config.notification,
Config.backgroundPermissionRationale.
Constructors
-
AppConfig({bool? stopOnTerminate, bool? startOnBoot, bool? enableHeadless, double? heartbeatInterval, List<
String> ? schedule, bool? scheduleUseAlarmManager, Notification? notification, PermissionRationale? backgroundPermissionRationale, bool? preventSuspend}) -
const
-
AppConfig.fromMap(Map<
String, dynamic> m) -
factory
Properties
- backgroundPermissionRationale → PermissionRationale?
-
(Android 11+) Configure the dialog presented to the user when Always location permission is requested.
final
- enableHeadless → bool?
-
[Android only]Enables "Headless" operation allowing you to respond to events after you app has been terminated with AppConfig.stopOnTerminate:false.final - hashCode → int
-
The hash code for this object.
no setterinherited
- heartbeatInterval → double?
-
Controls the rate (in seconds) the BackgroundGeolocation.onHeartbeat event will fire.
final
- notification → Notification?
-
(Android only) The Android operating system requires a persistent notification when running a foreground service.
final
- preventSuspend → bool?
-
[iOS only]Prevent iOS from suspending your application in the background after location-services have been switched off.final - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
schedule
→ List<
String> ? -
Configures an automated, cron-like schedule for the SDK to BackgroundGeolocation.start / BackgroundGeolocation.stop tracking at pre-defined times.
final
- scheduleUseAlarmManager → bool?
-
Android only Force the Android scheduler to use
AlarmManager(more precise) instead ofJobScheduler. Defaults tofalse.final - startOnBoot → bool?
-
Controls whether to resume location-tracking after device is rebooted.
final
- stopOnTerminate → bool?
-
Controls whether to continue location-tracking after application is terminated.
final
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toMap(
) → Map< String, dynamic> -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited