Sub topic 2
Choosing packages: geolocator and permission_handler
This chapter assumes you can add packages to a Flutter project, read pubspec.yaml, and write async/await code. It also assumes you know the basic idea of a runtime permission: the app asks, and the user answers. The next chapters cover declarations and the full request flow. Here you decide which packages to depend on and what each one gives you.
Two packages, two jobs
Flutter itself has no location API. Reading the device position and asking for permission both go through plugins that call native Android and iOS code. Two plugins from the Flutter community cover most apps.
- geolocator reads location. It can return the current position, the last known position, or a stream of updates. It also computes distances and bearings, and it can open the location settings screen. It includes its own permission methods, limited to location.
- permission_handler manages permissions. It covers camera, microphone, contacts, notifications, location, and many more, all behind one
PermissionAPI. It does not read the position.
The overlap is the source of the confusion. Both can check and request location permission. The question is whether you need the general permission tool or whether geolocator's built-in methods are enough.
What each package exposes
| Task | geolocator | permission_handler |
|---|---|---|
| Read current position | `Geolocator.getCurrentPosition()` | Not available |
| Stream position updates | `Geolocator.getPositionStream()` | Not available |
| Check location permission | `Geolocator.checkPermission()` | `Permission.locationWhenInUse.status` |
| Request location permission | `Geolocator.requestPermission()` | `Permission.locationWhenInUse.request()` |
| Check whether the location service is on | `Geolocator.isLocationServiceEnabled()` | `Permission.location.serviceStatus` |
| Open app settings | `Geolocator.openAppSettings()` | `openAppSettings()` |
| Open device location settings | `Geolocator.openLocationSettings()` | Not available |
| Request camera, microphone, and other permissions | Not available | Yes |
| Request "always" (background) location | `requestPermission()` after a while-in-use grant | `Permission.locationAlways.request()` |
The two packages describe the same permission with different vocabularies. geolocator returns a LocationPermission value: denied, deniedForever, whileInUse, always, or unableToDetermine. permissionhandler returns a PermissionStatus value: denied, granted, restricted, limited, permanentlyDenied, or provisional. A later chapter covers how to read these states. For now, notice that geolocator folds "granted" into whileInUse and always, while permissionhandler uses a single granted and tells you the scope through which Permission you asked about.
Option 1: geolocator alone
If your app only needs location, geolocator alone is the simplest choice. You add one dependency, and the permission API and the position API use one set of types.
flutter pub add geolocatorThis is the setup the Topic Spine's request-flow example uses. A single import gives you everything in that example: the service check, the permission check, the request, and the position read.
import 'package:geolocator/geolocator.dart';
Future<bool> canReadLocation() async {
if (!await Geolocator.isLocationServiceEnabled()) {
return false;
}
var permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
}
return permission == LocationPermission.whileInUse ||
permission == LocationPermission.always;
}Advantages of this option:
- One dependency to update and one changelog to read.
- Permission results map directly onto the position calls that can fail with them. Geolocator throws a
PermissionDeniedExceptionwhen you read a position without permission. - Fewer native build settings to configure.
The cost is scope. If your app later needs the camera or notifications, you will add permission_handler anyway, and you will then have two ways to ask for location permission in the same codebase.
Option 2: permission_handler plus geolocator
Choose this pairing when your app handles several kinds of permission and you want one consistent pattern for all of them. The permission flow lives in one place, built on Permission objects. Geolocator is then used only for what it does uniquely: reading positions.
flutter pub add geolocator permission_handlerA helper that uses both packages looks like this:
import 'package:geolocator/geolocator.dart';
import 'package:permission_handler/permission_handler.dart';
Future<Position?> readPositionOnce() async {
final status = await Permission.locationWhenInUse.request();
if (!status.isGranted) {
return null;
}
return Geolocator.getCurrentPosition(
locationSettings: const LocationSettings(accuracy: LocationAccuracy.high),
);
}Permission.locationWhenInUse.request() shows the system dialog only when the status is still undecided. If the permission is already granted, it returns immediately.
The extra iOS setup in permission_handler
permissionhandler on iOS compiles in support only for the permissions you name. Without that step, requests for a permission quietly return denied and the dialog never appears. You enable location by adding a macro to the `postinstall block of ios/Podfile`:
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
'PERMISSION_LOCATION=1',
]
end
end
endCheck the plugin's README for the current macro names, since they have changed between major versions. Geolocator needs no such step on iOS. This is a real advantage of Option 1.
Where the packages differ in behavior
Several differences affect how you write the flow.
- Service status. geolocator's
isLocationServiceEnabled()is a plain boolean. permission_handler'sserviceStatusreturns aServiceStatusvalue (enabled,disabled, ornotApplicable) that you compare against. Both ask the same question: is the device-wide location switch on? That is separate from whether your app has permission. - Settings screens. geolocator can open the location settings screen directly, so you can send a user whose service is off to the right place. permission_handler can only open your app's settings page.
- Android location provider. By default geolocator on Android uses the fused location provider from Google Play Services when it is available. It can fall back to the platform
LocationManager. You can force the platform manager through the Android-specific settings class, which matters on devices without Google Play Services. permission_handler has no say in this. - Precision on iOS 14 and later. Users can grant only approximate location. geolocator exposes this through
Geolocator.getLocationAccuracy()andrequestTemporaryFullAccuracy. A later chapter covers precision, so for now just note that this lives in geolocator.
A decision guide
Use these criteria, in order:
- Other permissions. If the app also needs camera, microphone, notifications, or contacts, include permission_handler and use it for every permission, including location.
- Setup cost. If location is the only permission, skip permission_handler. You avoid the Podfile macro and one dependency.
- Settings navigation. If you need to send users to the device location settings, you need geolocator for
openLocationSettings()either way. - Team consistency. If the codebase already wraps permissions behind one service class, extend that class and keep geolocator for positions only.
Mistakes to avoid
- Mixing both permission APIs in one flow. Calling
Geolocator.requestPermission()in one screen andPermission.location.request()in another is legal. It makes the code harder to follow and the status types hard to compare. Pick one for location and stick with it. - Using
Permission.locationwhen you mean when-in-use.Permission.locationis the general location permission, and the platform decides how it behaves.Permission.locationWhenInUsestates your intent. Use the specific one so the code says what it requests. - Assuming a request always shows a dialog. After a permanent denial, both packages return a result without prompting. A later chapter shows how to detect this and send the user to settings.
- Reading a position before checking the service. A granted permission does not mean the device can supply a position. If the service is off, the position call fails or waits. You need the service check regardless of which package you pick.
Putting it into pubspec.yaml
After running flutter pub add, the dependencies section contains entries like these. The version numbers will match whatever is current when you run the command.
dependencies:
flutter:
sdk: flutter
geolocator: ^13.0.0
permission_handler: ^11.3.0Commit the lock file, and update both packages with flutter pub upgrade after reading their changelogs. Major versions of both plugins have changed minimum Android SDK levels and Gradle requirements. If the Android build fails right after an upgrade, check compileSdk and the Android Gradle plugin version first.
Exercise
Your app records a delivery route using location and also lets drivers photograph the parcel with the camera. Which packages do you add, and which one do you use to request location permission?
Show answer
Add both geolocator and permissionhandler. Use geolocator for reading positions and streams. Use permissionhandler for every permission request, including location, so camera and location share one consistent pattern. This also means adding the Podfile macros for PERMISSION_LOCATION and the camera permission on iOS.
Exercise
A teammate installs permission_handler and calls Permission.locationWhenInUse.request() on iOS. The status always comes back denied and no dialog appears, although Info.plist contains the usage description. What is the most likely cause?
Show answer
The Podfile is missing the PERMISSIONLOCATION=1 preprocessor macro in the postinstall block, so permission_handler's location support is not compiled in on iOS. Add the macro, then run a clean build. If the app was already denied once, also reset the permission by reinstalling the app.