Sub topic 14
Error Handling and Testing the Permission Flow
Assume you know basic Dart, Flutter widgets, async/await, Futures, Streams, and how to edit the Android and iOS project folders. This chapter focuses on failures after the platform declarations and request flow are in place.
Treat each failure as a user-facing state
A location request can fail before the app reads any coordinates. Keep these cases separate so the interface can give useful guidance.
| Failure | Meaning | Useful response |
|---|---|---|
| Service disabled | Device-wide location is off | Explain how to enable Location |
| Denied | The user has not granted access | Explain the feature and offer another request |
| Permanently denied | The app cannot show the normal prompt again | Offer an app-settings link |
| Unavailable | The provider cannot produce a position | Retry or use a fallback |
| Unexpected error | A platform or package error occurred | Log details and show a general message |
Do not treat every failure as “permission denied.” A user who turned off the device location service needs different instructions from a user who rejected your app.
Separate the decision from the platform plugin
Dependency injection means passing a dependency into a class instead of constructing it inside the class. It lets tests supply fake platform responses.
import 'package:geolocator/geolocator.dart';
enum LocationFailure {
serviceOff,
denied,
permanentlyDenied,
}
class LocationAccess {
LocationAccess({
required this.serviceEnabled,
required this.currentPermission,
required this.requestPermission,
});
final Future<bool> Function() serviceEnabled;
final Future<LocationPermission> Function() currentPermission;
final Future<LocationPermission> Function() requestPermission;
Future<LocationFailure?> check() async {
if (!await serviceEnabled()) {
return LocationFailure.serviceOff;
}
var permission = await currentPermission();
if (permission == LocationPermission.denied) {
permission = await requestPermission();
}
if (permission == LocationPermission.denied) {
return LocationFailure.denied;
}
if (permission == LocationPermission.deniedForever) {
return LocationFailure.permanentlyDenied;
}
return null;
}
}The production object can use Geolocator.isLocationServiceEnabled, Geolocator.checkPermission, and Geolocator.requestPermission. The decision logic does not need a physical device.
Handle errors at the boundary
The widget should translate a failure into an action rather than display a raw exception. For example, serviceOff can show an explanation, while permanentlyDenied can call openAppSettings(). Catch errors from getCurrentPosition and position streams at the plugin boundary, record enough diagnostic detail for debugging, and show a short recovery message to the user.
Test every branch with fakes
A Flutter test can provide predictable responses:
import 'package:flutter_test/flutter_test.dart';
import 'package:geolocator/geolocator.dart';
void main() {
test('reports permanent denial without requesting again', () async {
var requestCalls = 0;
final access = LocationAccess(
serviceEnabled: () async => true,
currentPermission: () async => LocationPermission.deniedForever,
requestPermission: () async {
requestCalls++;
return LocationPermission.denied;
},
);
expect(
await access.check(),
LocationFailure.permanentlyDenied,
);
expect(requestCalls, 0);
});
}Add tests for a disabled service, an initial denial, a granted permission, and a denial returned after the request. Also test the position-reading method with a fake that throws a provider error.
Test on simulators and emulators
Automated tests verify your branching logic. An Android emulator or iOS Simulator verifies the real permission dialog, manifest or property-list configuration, and settings behavior. Reset app permissions between manual runs. Simulators can provide simulated locations, but hardware behavior such as weak signals, movement, and battery effects still requires a physical device.
Exercise
Why should a test use a fake permission provider instead of calling the device permission API?
Show answer
A fake makes each permission state deterministic, so the test can verify every branch without a simulator, physical device, or changing system settings.