Sub topic 1
What you are protecting against
Prerequisites
This chapter assumes you know basic Dart and async/await, can build a Flutter app for Android or iOS, can add packages with pub, and understand that an authentication token proves a user is signed in.
Threat model
Secure storage protects small secrets while the app is at rest on a device. The main threats are:
- A lost or stolen phone, where someone reads app files with a file browser or forensic tool.
- Another app on the same device trying to read your app's data.
- A backup or device migration that copies app data to another device.
Secure storage does not protect against a compromised running app, a malicious rooted or jailbroken device that can inspect memory, or a breach of your server. Treat it as one layer in a larger design.
What belongs in secure storage (and what does not)
Store only values whose exposure would let someone act as the user or reach private data.
| Value | Store in secure storage? |
|---|---|
| Refresh token | Yes |
| Short-lived access token | Yes |
| Display name or avatar URL | No |
| Cached API responses | No |
| Theme or language preference | No |
| User password | Never store it. Keep only the tokens that the server issues. |
Keep secure storage small. Each value adds a read to startup, and large payloads are not what the storage is built for.
How the package stores data on iOS and Android
The flutter_secure_storage package gives one Dart API over two native backends:
caption: How one Dart API reaches the iOS Keychain and the Android Keystore. alt: A Dart app calls the FlutterSecureStorage class, which sends data to the iOS Keychain or to Android encrypted storage whose key lives in Android Keystore.
On iOS, values go into the Keychain, which the operating system manages. On Android, the package encrypts each value with a key held in the Android Keystore and then writes the encrypted result to app storage.
Adding flutter_secure_storage to your app
Add the package with pub:
flutter pub add flutter_secure_storageThen import it where you need it:
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
const storage = FlutterSecureStorage();The next chapter covers the read, write, and delete operations.
Exercise
Which of these should go into secure storage: a refresh token, a user's display name, or a cached list of messages?
Show answer
Only the refresh token. The display name and cached messages are not secrets whose exposure grants access, so ordinary storage is enough for them.