Sub topic 7
Explaining why you need location
This chapter covers the user-facing side of a location request: when to ask, what to say before the system dialog appears, and how to word the text the operating system shows. It assumes you can already write the check-then-request flow from the previous chapter, you know the permission states (denied, granted, permanentlyDenied), and you are comfortable with async/await and basic Flutter dialogs.
Why explanation matters
The system permission dialog is short and fixed. Android shows "Allow [app] to access this device's location?" and iOS shows your usage description string. You control very little of that text, and users decide in a few seconds.
The cost of a bad moment is high:
- On iOS, the system dialog appears once. If the user taps "Don't Allow", calling the request again does nothing. Only the Settings app can change the answer.
- On Android, two denials are treated as a lasting "don't ask again" on recent versions.
So you have a small number of chances. A clear explanation, shown at the right moment, raises the grant rate and respects the user's choice.
Two places to explain
You can explain in two places. They do different jobs.
| Place | Who writes it | When it shows | What it is for |
|---|---|---|---|
| Usage description (`Info.plist`) | You, as a string | Inside the iOS system dialog | Short reason, tied to the feature |
| In-app rationale screen or dialog | You, as Flutter UI | Before you call the system request | Longer explanation, a "Not now" option |
The in-app rationale is sometimes called a "soft ask" or "pre-permission prompt". Its main benefit is that a "Not now" answer costs nothing. The user has not denied anything at the OS level, so you can ask again later.
Writing the iOS usage description
The string in Info.plist is part of the iOS dialog. App Store review also reads it. A vague string can lead to a rejection.
| Weak | Better |
|---|---|
| `We need your location.` | `We use your location to show nearby places while the app is open.` |
| `Required for app functionality.` | `Your location lets us sort stores by distance. It is not stored.` |
A good string names the feature, says when location is used, and does not promise anything the app does not do. If the app only reads location in the foreground, use NSLocationWhenInUseUsageDescription and say "while the app is open".
<key>NSLocationWhenInUseUsageDescription</key>
<string>We use your location to show nearby places while the app is open.</string>Android has no equivalent string. The dialog text is fixed by the system, so your in-app rationale is the only place to explain there.
Ask in context
Do not request location in main() or on the first screen. The user has no reason yet to trust the app, and the dialog has no visible connection to anything they did.
Ask when the user taps something that clearly needs location, such as "Find places near me". The dialog then reads as a consequence of their action. If the app is useful without location, offer a manual alternative, for example a text field for a city. The user can then decline without losing the feature.
What Android tells you: the rationale flag
On Android, the system tracks whether you should explain before asking again. The permission_handler package exposes this as shouldShowRequestRationale.
- It returns
trueafter the user has denied the permission once and has not chosen "Don't ask again". - It returns
falsebefore the first request, and after a lasting denial. - On iOS it always returns
false.
Because it is false both before the first ask and after a lasting denial, you cannot use it alone to decide whether to ask. Combine it with status.
import 'package:permission_handler/permission_handler.dart';
Future<void> inspectLocation() async {
final status = await Permission.locationWhenInUse.status;
final rationale =
await Permission.locationWhenInUse.shouldShowRequestRationale;
print('status=$status, shouldExplain=$rationale');
}A reading of denied with shouldExplain=true means the user said no once on Android. That is the best moment for a stronger explanation, because they have shown hesitation.
A rationale-first request
The code below shows a dialog, then calls the system request only if the user agrees. It covers three outcomes: granted, declined at your dialog, and permanently denied. The permanent case only reports false here. The next chapter covers the settings redirect.
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
Future<bool> _askUserToContinue(
BuildContext context, {
required bool previouslyDenied,
}) async {
final proceed = await showDialog<bool>(
context: context,
barrierDismissible: false,
builder: (dialogContext) => AlertDialog(
title: const Text('Find places near you'),
content: Text(
previouslyDenied
? 'Without location we cannot sort places by distance. '
'We only read it while the app is open.'
: 'We use your location to show nearby places. '
'We only read it while the app is open.',
),
actions: [
TextButton(
onPressed: () => Navigator.pop(dialogContext, false),
child: const Text('Not now'),
),
FilledButton(
onPressed: () => Navigator.pop(dialogContext, true),
child: const Text('Continue'),
),
],
),
);
return proceed ?? false;
}
Future<bool> requestLocationWithRationale(BuildContext context) async {
final permission = Permission.locationWhenInUse;
final current = await permission.status;
if (current.isGranted) {
return true;
}
if (current.isPermanentlyDenied) {
return false;
}
final previouslyDenied = await permission.shouldShowRequestRationale;
if (!context.mounted) {
return false;
}
final agreed = await _askUserToContinue(
context,
previouslyDenied: previouslyDenied,
);
if (!agreed) {
return false;
}
final result = await permission.request();
return result.isGranted;
}Points to notice:
- The function checks
statusfirst, so a user who already granted permission never sees the dialog again. context.mountedis checked after anawait, because the widget may have left the tree while the status was read.- "Not now" returns
falsewithout touching the OS. Nothing is recorded as a denial. - The
previouslyDeniedflag changes the wording, not the flow.
The decision flow
Common mistakes
- Explaining after the fact. A dialog saying "we need location" after the user denied it can feel like pressure. Explain before the request.
- Asking again and again. If the user chose "Not now" or denied, do not show the dialog on every screen. Wait for the user to tap the feature again, or keep a small inline hint instead.
- A blocking wall. A full-screen "You must allow location" with no exit pushes users to uninstall. Always give a way to continue without location when the feature allows it.
- Overpromising in the text. Saying "never stored" when you send coordinates to a server breaks trust and may violate store policy. Describe actual behavior.
- Asking for more than needed. If you only need a city-level result, say so and ask for approximate precision. The accuracy chapter covers this.
Testing the wording
Test the explanation on a real flow, not only in code:
- Install a fresh build, or reset permissions on an emulator or simulator, so the status is
deniedwith no history. - Tap the feature and read your dialog, then the system dialog. Check that the two do not contradict each other.
- Choose "Not now" and confirm no system dialog appears and the manual alternative works.
- Deny at the system dialog, tap the feature again, and confirm the stronger wording appears on Android. On iOS, confirm that the app does not show a dialog that leads nowhere.
Exercise
Your Android app reads status denied and shouldShowRequestRationale as false. What are the two situations this can mean, and why can't you decide what to do from the flag alone?
Show answer
Either the app has never asked yet, or the user has chosen a lasting denial ("Don't ask again" or repeated denials). The flag is false in both cases. To tell them apart you need extra information, such as a stored "already asked" value or the permanentlyDenied status, and you should treat permanentlyDenied as a case for the settings redirect instead of another request.
Exercise
A teammate wants to show the in-app rationale dialog as soon as the app starts, so that "everyone sees it once". Give two reasons to move it, and say where it should go instead.
Show answer
At startup the user has not done anything that needs location, so the request has no visible cause and users are more likely to refuse. A refusal at the system dialog is hard to undo, especially on iOS. Move the dialog to the moment the user taps a location-dependent feature, such as "Find places near me", and offer a manual alternative if they choose "Not now".