Sub topic 3
How the package stores data on iOS and Android
This chapter assumes you know basic Dart, async and await, Flutter packages, and the purpose of authentication tokens. The goal is to understand the storage model before choosing options or writing a token repository.
flutter_secure_storage provides one Dart API for several platform-specific storage systems. Your Flutter code calls methods such as write and read. The package's native code then stores the value using a protected platform service.
The iOS backend: Keychain Services
On iOS, the package stores values in the Keychain. The Keychain is an operating-system service designed for passwords, keys, certificates, and other small secrets.
The app does not normally receive a file path or a database row. Instead, it asks Keychain Services to save or retrieve an item identified by attributes such as a key and service name. iOS controls access to that item.
Keychain accessibility is important. An accessibility setting determines when an item can be read:
first_unlockallows access after the user unlocks the device once after a restart.- A stricter setting can require the device to be unlocked whenever the app reads the item.
- Some settings affect whether an item can be accessed while the device is locked.
For example:
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
const storage = FlutterSecureStorage(
iOptions: IOSOptions(
accessibility: KeychainAccessibility.first_unlock,
),
);Choose the least permissive setting that supports the app's behavior. A background task may need a different setting from a screen that reads a token only after the user opens the app.
The Android backend: Keystore-backed protection
Android uses the Keystore to protect cryptographic keys. A Keystore key is held and managed by the operating system. The secret value itself is stored through the package's Android storage implementation, while encryption keys are protected by Keystore facilities.
This separation matters. A copied preferences file should not be enough to recover its plaintext values without the corresponding protected key.
Android behavior depends on the package version and its configured options. Older configurations commonly enabled encrypted shared preferences explicitly:
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
const storage = FlutterSecureStorage(
aOptions: AndroidOptions(
encryptedSharedPreferences: true,
),
);Check the version's current package documentation before selecting this option. Some newer versions change defaults or deprecate older configuration flags. Do not copy an Android option from an old example without checking its compatibility with your installed version.
| Platform | Protected system service | Main configuration concern |
|---|---|---|
| iOS | Keychain Services | When the item is accessible |
| Android | Keystore-backed encrypted storage | Package version and encryption options |
What this abstraction does not hide
The Dart API hides platform calls, but it does not make both platforms identical.
- An iOS accessibility choice has no direct Android equivalent.
- Android backup and device migration behavior can differ from iOS Keychain behavior.
- A restored backup may contain encrypted data that no longer matches the device's protected keys.
- A reinstall may preserve or remove data according to platform behavior and package configuration.
- Storage access is asynchronous because the call crosses from Dart into native platform code.
Treat a read failure as a real authentication state. If an old value cannot be decrypted, continuing with it is unsafe. A later chapter will wrap this behavior in a repository and clear unusable data.
Exercise
Why can the same read call behave differently after a device restore or on a locked iPhone?
Show answer
The call uses platform-managed keys and access rules. A restore may not include the matching protected key, and iOS Keychain accessibility settings may forbid reading the item while the device is locked.