Sub topic 6
Keeping Secrets in Secure Storage
When plain preferences are not enough
Assume you know basic Dart classes, null safety, and asynchronous code with Future, async, and await. You should also know how to add a package to pubspec.yaml.
A setting such as a theme choice is not usually sensitive. An authentication token is different. If an attacker reads that token, they may be able to act as the user. Do not store tokens, passwords, private keys, or recovery codes in ordinary preferences or an unencrypted JSON file.
flutter_secure_storage is a Flutter package that stores small secret values through platform-provided secure storage. On iOS, this normally uses the Keychain. On Android, it uses secure platform mechanisms backed by the Android Keystore. Your Dart code uses one API while each platform protects the value differently.
Secure storage reduces the risk of another app or a casual file inspection reading the value. It does not protect a token while your application is running on a compromised device, and it does not replace server-side token expiration and revocation.
Writing, reading, and deleting a token
Add flutter_secure_storage to pubspec.yaml, then run flutter pub get. A small storage service can expose only the operations that the rest of the app needs:
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
class TokenStore {
const TokenStore([this._storage = const FlutterSecureStorage()]);
final FlutterSecureStorage _storage;
Future<void> saveAccessToken(String token) {
return _storage.write(key: 'access_token', value: token);
}
Future<String?> readAccessToken() {
return _storage.read(key: 'access_token');
}
Future<void> clearAccessToken() {
return _storage.delete(key: 'access_token');
}
}readAccessToken returns String? because the key may not exist. A missing value is normal on the first launch or after sign-out. Treat it as an unauthenticated state rather than forcing it to a non-null string.
A typical sign-in flow saves the token only after the server accepts the credentials. An API client can read the token before making an authenticated request. Sign-out should delete the token, not merely overwrite it with an empty string.
Secure storage boundaries and failure modes
Secure storage is intended for small values. Do not use it as a database for a large object list. Store structured application data in a file or database, and keep only the sensitive credential in secure storage.
The value can become unavailable after an operating-system restore, a device migration, an app reinstall, or a change in platform security settings. Code that reads a token must handle errors and be able to request authentication again.
Use a token with a limited lifetime when the server supports it. Prefer a refresh-token flow in which the app stores the refresh token securely and obtains short-lived access tokens. Never print token values in logs, error messages, analytics events, or crash reports.
How the token flow works
Keep the storage API behind a class such as TokenStore. This gives the rest of the application a clear dependency and makes it easier to replace the package or provide a fake store in tests.
Exercise
Exercise
Why should readAccessToken return String? instead of String, and what should sign-out do with the stored value?
Show answer
The key may not exist, so the result is nullable. Sign-out should delete the key so the old credential cannot be reused by the app.