Sub topic 4
Adding `flutter_secure_storage` to Your Flutter App
This chapter assumes you know basic Dart, async and await, Flutter project structure, and how to add packages with pub. You should also have a Flutter app that builds for Android or iOS and understand the purpose of an authentication token.
flutter_secure_storage is a Flutter plugin for storing small secret values. It calls a platform-specific secure storage system instead of saving values in an ordinary Dart file or an unencrypted preferences store.
Add the package
From the root directory of your Flutter project, run:
flutter pub add flutter_secure_storageThis command updates pubspec.yaml with the latest package version compatible with your project and downloads the package. You can also add the dependency manually, but the command avoids common version and formatting errors.
After adding a plugin, stop and restart the application. A hot reload is not always enough because Flutter must register the plugin with the native platform.
Import the package in the Dart file that needs secure storage:
import 'package:flutter_secure_storage/flutter_secure_storage.dart';The package exposes a FlutterSecureStorage object. Its methods are asynchronous because they communicate with native platform code.
Understand the platform backends
On iOS, the plugin uses the Keychain. On Android, it uses Android secure-storage mechanisms backed by the Android Keystore. Your Dart code uses the same API on both platforms, while the native implementation handles the platform-specific details.
The package is designed for small values such as tokens, keys, and short credentials. It is not a general database. Keep large application data in a database or another appropriate store.
Create a storage instance
The simplest setup uses the default platform configuration:
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
const storage = FlutterSecureStorage();
Future<void> saveExample() async {
await storage.write(
key: 'refresh_token',
value: 'abc123',
);
}The key identifies the value. Both the key and value are strings. write returns a Future, so call it with await inside an asynchronous function.
A single shared instance is usually enough for an application. Create it near the boundary of your data layer, such as in a repository, rather than constructing unrelated instances throughout the widget tree.
Configure native options
Platform options are passed to the constructor. For example, this configuration allows an iOS Keychain item to be read after the first device unlock:
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
const storage = FlutterSecureStorage(
iOptions: IOSOptions(
accessibility: KeychainAccessibility.first_unlock,
),
aOptions: AndroidOptions(
encryptedSharedPreferences: true,
),
);KeychainAccessibility.first_unlock matters for applications that need access while the device is locked after it has been unlocked once. Other accessibility choices provide different security and availability trade-offs.
The Android encryptedSharedPreferences option is supported by package versions that expose it. Newer versions may deprecate or change this option. Check the package README and migration notes for the version installed in your project before copying a platform configuration into production.
| Option | Platform | Main decision |
|---|---|---|
| `accessibility` | iOS | When the Keychain item may be read |
| `encryptedSharedPreferences` | Android | Whether to use the package's encrypted preferences mode |
Check the native build
Run the application on each target platform you support:
flutter runIf the build fails, read the first native error rather than changing Dart code at random. Common causes include an outdated Flutter SDK, an unsupported Android or iOS deployment target, or a package version whose native requirements do not match the project.
Use these commands to inspect the project and dependency state:
flutter doctor
flutter pub outdated
flutter pub getDo not commit tokens to pubspec.yaml, source code, logs, or test fixtures that are shared publicly. The package protects stored values on the device, but it cannot protect a secret that your application distributes to every user.
Exercise
Which command adds flutter_secure_storage to an existing Flutter project, and why should you restart the app after adding it?
Show answer
Run flutter pub add flutter_secure_storage. Restart the app so Flutter can register and load the plugin's native implementation.