Sub topic 13
Background Location and Its Extra Requirements
Assume you know Dart async and await, Flutter lifecycle methods, Streams, and how to edit a Flutter project's Android and iOS folders. This chapter focuses on location updates while the app is not visible.
Background location has three separate concerns:
- Permission: the user must authorize the required level of access.
- Platform configuration: Android and iOS need declarations and capabilities.
- Execution: the operating system decides when your app may run and how long it may continue.
A location stream that works while a screen is open does not automatically work after the user locks the phone.
Android requirements
Android separates foreground access from background access. Foreground access is enough while the app is visible. Continuous updates while the app is in the background usually require ACCESS_BACKGROUND_LOCATION on Android 10 and later.
Add only the permissions your feature needs:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<application
android:label="Route tracker"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<!-- Activities and services go here. -->
</application>
</manifest>Android may not show background access in the same dialog as foreground access. On recent versions, the user often enables it from a system settings screen. Request foreground access first, explain the feature, and request background access only when the user starts a feature that truly needs it.
Long-running tracking may also require a foreground service. A foreground service displays a persistent notification and tells the user that tracking is active. Its declaration, notification behavior, service type, and permissions depend on the Android version and target SDK. Android 14 and later add stricter foreground-service rules, including a location service type.
iOS requirements
For iOS, add a clear purpose string and enable the Location updates background mode in the Runner target. When the feature needs access beyond the app being visible, include the appropriate Always authorization description.
<key>NSLocationWhenInUseUsageDescription</key>
<string>We use your location to record your route.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>We use your location to record your route while the app is in the background.</string>
<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>In Xcode, also open the Runner target's Signing and Capabilities tab and add Background Modes with Location updates enabled. The user still controls authorization. A purpose string does not grant access.
Configure the stream deliberately
With geolocator, use platform settings when background behavior needs explicit configuration. For example, enable the iOS background update option and use an Android foreground notification for active tracking. The exact options can change between package versions, so check the version's API documentation before upgrading.
Choose a suitable accuracy and distance filter. High accuracy and frequent updates use more battery. If a delivery route only needs a new point every 50 meters, do not request a point every second.
Lifecycle, denial, and review constraints
Keep the StreamSubscription<Position> while tracking is active and cancel it when the user stops. Do not leave a subscription running because a screen was disposed. When the app returns to the foreground, recheck permission and the location service. The user may have changed either setting in system settings.
Background behavior can also be interrupted by low battery, force-quit actions, OS scheduling, missing notifications, or a platform policy change. Do not promise uninterrupted delivery. If approximate location is sufficient, accept it. If the user denies background access, keep the foreground feature usable where possible.
Apple and Google review background location closely. Your store listing, in-app explanation, permission request, and actual behavior should describe the same feature. A feature that uploads location without a clear user action is likely to receive extra scrutiny or rejection.
Exercise
A route recorder works while its screen is open but stops when the phone is locked. Name two categories of changes you should investigate before changing Dart stream code.
Show answer
Check platform configuration and authorization first. The app may lack background permission or a required background mode, or Android may require a foreground service and notification.