Sub topic 7
Keychain Accessibility and Android Options
This chapter assumes basic Dart, async and await, a Flutter app that builds for Android or iOS, and familiarity with authentication tokens. The package stores small secret values through platform security services, but platform options decide when those values are available and how Android encrypts them.
Choosing iOS Keychain accessibility
On iOS, flutter_secure_storage stores values in the Keychain. The accessibility option controls when the operating system permits an app to read a Keychain item.
Common choices include:
| Option | Meaning | Typical use |
|---|---|---|
| `when_unlocked` | Readable only while the device is unlocked | Secrets needed after the user opens the app |
| `first_unlock` | Readable after the first device unlock following boot | Background work that runs while the screen is locked |
| `after_first_unlock` | Similar availability for background access after the first unlock | Services that must continue after reboot |
The exact enum names and available choices depend on the package version. Check the package API for the version in your pubspec.yaml.
A stricter accessibility setting reduces the time during which a secret is available. A less restrictive setting supports background tasks but exposes the secret to more device states. Choose based on when your app genuinely needs the value.
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
const storage = FlutterSecureStorage(
iOptions: IOSOptions(
accessibility: KeychainAccessibility.first_unlock,
),
);first_unlock does not mean that the user must authenticate with Face ID or Touch ID each time. It means the device must have completed its first unlock after boot. Use a separate biometric package when the user must approve each sensitive action.
Configuring Android storage
On Android, the package uses the Android Keystore with encrypted application storage. The Keystore protects encryption keys using hardware-backed or system-managed security when the device supports it.
Android configuration is passed through AndroidOptions. Some package releases expose encryptedSharedPreferences, which selects an encrypted preferences backend:
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
const storage = FlutterSecureStorage(
aOptions: AndroidOptions(
encryptedSharedPreferences: true,
),
);Package APIs change over time. In newer releases, this flag may be deprecated because the encrypted backend is already the default or because the package has changed its implementation. Follow the README and migration notes for the version your app uses. Do not copy an old option into a new project without checking its status.
Use one consistently configured storage instance. Mixing constructors with different Android settings can make migrations and debugging harder.
How the options affect app behavior
The platform stores the value, but your Dart code still has to handle availability failures. A read can fail after events such as an operating-system restore, a changed device security state, or incompatible old data.
Selecting options for a token
A refresh token normally needs to survive app restarts, but it should not be required before the user unlocks the device. The default locked-device behavior is often a suitable starting point for foreground authentication. Choose first_unlock only when background work must refresh data before the user opens the app.
On Android, use the current package defaults unless you have a specific compatibility requirement. Test on the Android versions and device types that your app supports, including devices with and without hardware-backed Keystore support.
Exercise
Your app refreshes an access token only after the user opens it. Should you select iOS first_unlock solely to make the token more secure?
Show answer
No. first_unlock enables access while the device is locked after its first unlock. Use a setting that matches the app's actual lifecycle, and add biometric gating separately if a user approval is required.