Sub topic 5
Writing, reading and deleting values
This chapter assumes basic Dart, async/await, and a Flutter app that already uses packages from pubspec.yaml. It also assumes you know that an authentication token is a credential that represents a signed-in session.
flutter_secure_storage stores small values under string keys. A key is the name used to find a value later. The package exposes asynchronous methods because the data is handled by a platform storage system, not by ordinary Dart memory.
Add the package
From your Flutter project directory, run:
flutter pub add flutter_secure_storageThen import the package where you need secure storage:
import 'package:flutter_secure_storage/flutter_secure_storage.dart';Create a storage object. A FlutterSecureStorage instance provides methods for writing, reading, and removing values.
const storage = FlutterSecureStorage();Write and read a value
Use write with a string key and a string value. Because the operation is asynchronous, call it with await inside an async function.
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
const storage = FlutterSecureStorage();
Future<void> demo() async {
await storage.write(key: 'refresh_token', value: 'abc123');
final token = await storage.read(key: 'refresh_token');
print(token); // abc123
}Writing another value with the same key replaces the existing value. This makes a stable key important. Put key names in one place instead of repeating string literals throughout the application.
read returns a nullable String. If the key does not exist, the result is null, so your authentication code must handle that case.
final token = await storage.read(key: 'refresh_token');
if (token == null) {
print('The user is signed out.');
} else {
print('Use the token to refresh the session.');
}A missing value is different from an empty string. A missing value usually means that nothing has been stored or that the value was removed. An empty string is still a stored value, although it is usually invalid for a token.
Read all values
Use readAll when you need every stored key and value:
final values = await storage.readAll();
print(values); // {refresh_token: abc123}The result is a Map<String, String>. Avoid calling readAll merely to retrieve one token. read communicates your intention more clearly and limits the data your code handles.
Delete values
Delete one value with delete:
await storage.delete(key: 'refresh_token');
final token = await storage.read(key: 'refresh_token');
print(token); // nullUse deleteAll only when every stored value belongs to the same application and should be removed. It is useful during a complete sign-out, but it can remove unrelated values that your code has stored.
await storage.deleteAll();A sign-out flow should remove refresh tokens and other session secrets. It should not assume that deleting a local token also invalidates the token on the server. If possible, sign out from the server first, then clear the local value even when the server request fails.
Common mistakes
- Forgetting
await, which can cause later code to run before the write finishes. - Reading with a different key from the one used during writing.
- Assuming
readalways returns a non-null string. - Storing large objects or ordinary preferences that do not need secrecy.
- Logging secret values while debugging.
- Calling
deleteAllwhen only one credential should be removed.
Exercise
What does read return after delete(key: 'refresh_token'), and why should the result be checked?
Show answer
It returns null because the key no longer exists. The result should be checked because a missing token normally means the user must sign in again.