Sub topic 2
Choosing Packages: `geolocator` and `permission_handler`
This chapter assumes you know basic Flutter widgets, Dart async/await, Future, and Stream, and that you can edit pubspec.yaml and the Android and iOS folders.
Flutter does not provide one built-in API for reading device location. A plugin connects Dart code to Android and iOS location APIs. Two common choices are geolocator and permission_handler.
What each package provides
geolocator focuses on location. It can check whether the device's location service is enabled, request location permission, read the current position, and subscribe to position updates.
permission_handler provides a general permission API. It can request location, camera, microphone, notification, and other permissions through a similar interface. It can also open the app's settings page when a permission is permanently denied.
| Package | Best fit | Main strength |
|---|---|---|
| `geolocator` | An app whose main platform feature is location | Combines permission checks with position APIs |
| `permission_handler` | An app that manages several permission types | Gives many permissions one consistent API |
| Both packages | A deliberate, shared permission architecture | Separates permission policy from location operations |
Using both packages is possible, but it creates two APIs for related permission work. Decide which package owns permission requests. For a small location-only app, using geolocator alone is usually simpler. For an app that already uses permission_handler for several permissions, keep permission decisions there and use geolocator for location data.
Add the selected package
Add the dependency to pubspec.yaml, then run flutter pub get.
dependencies:
flutter:
sdk: flutter
geolocator: ^13.0.0The exact current version can change. Check the package's release information before choosing a version for a new project. In a team project, commit pubspec.lock where the project convention requires it.
With geolocator, a location request can remain in one flow:
import 'package:geolocator/geolocator.dart';
Future<Position> readPosition() async {
if (!await Geolocator.isLocationServiceEnabled()) {
throw StateError('Turn on location services.');
}
var permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
}
if (permission == LocationPermission.denied ||
permission == LocationPermission.deniedForever) {
throw StateError('Location permission is unavailable.');
}
return Geolocator.getCurrentPosition();
}This function is intentionally small. A production app should present a useful message in the user interface and distinguish temporary denial from permanent denial.
If your app already centralizes permission handling, use permission_handler for the request:
import 'package:permission_handler/permission_handler.dart';
Future<bool> requestLocationAccess() async {
final status = await Permission.locationWhenInUse.request();
if (status.isGranted) {
return true;
}
if (status.isPermanentlyDenied) {
await openAppSettings();
}
return false;
}After access is granted, geolocator can still read the position. Keep this boundary clear: one component decides whether access is allowed, and another reads location data.
Choose by ownership and scope
Use these questions before adding a package:
- Does the app need only location, or several permission types?
- Where will the app explain why it needs access?
- Which component handles permanent denial and settings navigation?
- Will the app need a location stream or background tracking later?
- Can tests replace the plugin behind an application-owned interface?
An application-owned interface prevents widgets from depending directly on a plugin:
Exercise
Your app only needs the current position and a position stream. Which package is the simplest default, and when might you add permission_handler?
Show answer
Start with geolocator alone. Add permission_handler when the app already manages several permission types through one shared permission policy.