Sub topic 3
How the package stores data on iOS and Android
flutter_secure_storage has one Dart API, but it does not store data the same way on each platform. It hands your values to the operating system's own secret-keeping facilities. This chapter explains what happens after write is called, so you can predict the package's behavior on reinstalls, backups, and locked devices.
This chapter assumes you have read the threat model chapter and know how Flutter platform channels carry calls from Dart to native code. You don't need to write Swift, Kotlin, or Java.
The shared shape: Dart over a platform channel
The Dart code in the package is thin. Each call (write, read, delete, readAll, deleteAll) is sent over a platform channel to native plugin code. That code does the real work using the APIs of the host operating system.
Because of this split, the Dart signature tells you very little about the security properties. Those come from the native layer, and they differ between platforms.
iOS: Keychain items
On iOS, the package uses Keychain Services. Each key-value pair becomes one Keychain item of class generic password. The item has these attributes:
- Account: your string key, such as
refresh_token. - Service: a grouping label. The package uses a default service name, and you can change it with
IOSOptions(accountName: ...). - Value data: your string, stored as UTF-8 bytes.
- Accessibility: a rule for when the OS lets the item be read.
The operating system encrypts Keychain data. The package does no encryption of its own on iOS. Keys are managed by the system and tied to the device, and on devices with a Secure Enclave the key hierarchy is protected by it. Your app never handles an encryption key.
Accessibility controls when the value is readable
The accessibility attribute is the most important iOS setting. It is exposed as KeychainAccessibility:
| Value | Readable when | Moves to a new device via backup |
|---|---|---|
| `passcode` | Device unlocked, and only if a passcode is set | No |
| `unlocked` | Device unlocked | Yes (encrypted backup) |
| `unlocked_this_device` | Device unlocked | No |
| `first_unlock` | After the first unlock since boot | Yes (encrypted backup) |
| `first_unlock_this_device` | After the first unlock since boot | No |
The default is unlocked. This is the strictest choice that still allows backup restore, and it is a good fit for foreground-only apps. If your app reads a secret while the device is locked, unlocked will fail. Examples are a background fetch, a push-notification handler, or a background isolate. For those cases use first_unlock, or first_unlock_this_device if the secret must not leave the device.
The _this_device variants are what you want for tokens that should not follow the user to a new phone. A restored backup then arrives without them, and the user signs in again.
Keychain items can outlive the app
On iOS, Keychain items generally stay on the device after the app is uninstalled. If the user reinstalls the app, read can return a token that the new install never wrote. Android behaves differently, as the next section shows. The reinstall behavior has practical consequences. The lifecycle chapter covers them, so this chapter only notes that the cause is the Keychain's storage model, which is separate from the app's sandbox.
Android: Keystore-protected encryption over SharedPreferences
Android has no direct counterpart to the Keychain's generic password store for app strings. The package therefore combines two pieces:
- The Android Keystore, a system service that generates and holds cryptographic keys. Depending on the device, key material lives in secure hardware (a trusted execution environment or a StrongBox chip) and is never exposed to the app. The app can ask the Keystore to use a key, but it cannot read the key's bytes.
- SharedPreferences, an ordinary XML key-value file in the app's private storage.
Your value is encrypted using a Keystore-protected key. The encrypted bytes are then saved in SharedPreferences. The file on disk holds ciphertext, and decryption needs a key that only the Keystore can use.
Two implementations
The package has used two approaches on Android, and which one applies depends on the package version and your options.
- Package-managed ciphers. The plugin creates a key in the Keystore and uses it to protect a data key or to encrypt values directly. It then writes the result to its own SharedPreferences file. Key names in this scheme are stored as plain preference names. Only the values are encrypted.
- Jetpack
EncryptedSharedPreferences. SettingAndroidOptions(encryptedSharedPreferences: true)delegates to the AndroidX Security library. It encrypts both the key names and the values, using a master key held in the Keystore.
Newer major versions of the package have been moving away from EncryptedSharedPreferences because the AndroidX library is deprecated, and the encryptedSharedPreferences flag may be marked as deprecated or ignored. The default cipher choices have also changed between versions. Read the README and changelog for the version in your pubspec.lock before you rely on a specific scheme. Don't write migration code against a scheme you haven't confirmed.
Naming the storage
You can name the iOS service and the Android preferences file. This is useful when two features of one app need separate stores, or when you want to avoid collisions with another plugin.
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
const storage = FlutterSecureStorage(
iOptions: IOSOptions(
accountName: 'my_app_secrets',
accessibility: KeychainAccessibility.first_unlock_this_device,
),
aOptions: AndroidOptions(
sharedPreferencesName: 'my_app_secure_prefs',
preferencesKeyPrefix: 'my_app_',
),
);
Future<void> main() async {
await storage.write(key: 'api_key', value: 'example-value');
print(await storage.read(key: 'api_key'));
}Two instances configured with different names are stored in separate places. If you change these names after release, existing data is no longer found under the new name. Choose them once.
Why the Android file can be copied but not read
The Android design explains one of the package's best-known failure modes. The ciphertext lives in a normal file, which means Android's Auto Backup can include it in a cloud backup. The encryption key lives in the Keystore, which is bound to the physical device and is not part of that backup.
After a restore onto a new phone, the ciphertext is present but the key is gone. A read call then fails during decryption and throws a PlatformException. This is why the repository chapter catches that exception and treats it as "signed out." You can also exclude the file from backup in your Android manifest.
iOS has the opposite problem. The data is protected, but whether it migrates depends on the accessibility value you chose. On Android the key never migrates, and on iOS you decide.
Side by side
| Property | iOS | Android |
|---|---|---|
| Where the value rests | Keychain database | SharedPreferences file (ciphertext) |
| Who encrypts | The operating system | The plugin, or AndroidX, with a Keystore key |
| Where the key lives | System-managed, device-bound | Android Keystore, often hardware-backed |
| Key names visible on disk | Not applicable | Yes with package-managed ciphers, no with `EncryptedSharedPreferences` |
| Survives uninstall | Often yes | No, the app's private files are deleted |
| Main way to control access | `KeychainAccessibility` | Cipher options and backup rules |
| Restore to a new device | Depends on accessibility | Ciphertext restores, key does not |
Android's last two rows follow from the design above. The Keystore key for an uninstalled app is deleted along with its data, so a reinstall starts clean.
What these backends do not give you
Both backends protect data at rest against other apps and against someone who copies the device's storage. They do not protect against code running as your app. A rooted or jailbroken device, a debugger attached to your process, or an attacker who can run your own code can ask the same APIs to decrypt the value. Once read has returned a token to Dart, that string is in ordinary memory.
Summary of what to remember
- On iOS the package stores each pair as a generic password Keychain item, and the OS does the encryption.
- On Android the package stores ciphertext in SharedPreferences, with the key held by the Keystore.
KeychainAccessibilitydecides when an iOS value can be read and whether it moves to a new device.- The Android Keystore key stays on the device, so restored ciphertext can become unreadable.
- Package versions change the Android implementation, so verify the scheme for the version you use.
Exercise
A user installs your app, signs in, and the refresh token is saved with the default options. They then restore a cloud backup onto a new Android phone. What does read(key: 'refresh_token') do on the new phone, and why? What would be different on an iPhone restored from an encrypted backup if you had used KeychainAccessibility.first_unlock_this_device?
Show answer
On Android the encrypted value in SharedPreferences may be restored by Auto Backup, but the Keystore key does not leave the old device. Decryption fails, so read throws a PlatformException, and the app should catch it, clear the stored data, and treat the user as signed out. On iOS, first_unlock_this_device items are not migrated to a new device through backup. The token would be missing, read would return null, and the user would also need to sign in again, but without an exception.