Sub topic 3
Handling platform errors
This chapter assumes you know Dart async/await, have a Flutter app that builds for Android or iOS, and can add packages with flutter pub add.
Handling platform errors
The package calls native iOS and Android code through a platform channel. When the native side fails, it throws a PlatformException. A common cause is stored data the device can no longer decrypt, such as data restored from a backup onto a different device.
import 'package:flutter/services.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
Future<String?> readTokenSafely(FlutterSecureStorage storage) async {
try {
return await storage.read(key: 'refresh_token');
} on PlatformException {
await storage.deleteAll();
return null;
}
}Returning null lets the app treat the user as signed out. Clearing the store prevents the app from failing on the same bad data every launch.
Reinstalls, backups and sign-out
iOS Keychain items can outlive an app uninstall, so a reinstalled app may still find an old token. Android backups can restore encrypted values that the new device cannot read. Handle both cases with a first-launch check. Store a plain flag in ordinary preferences, and call deleteAll() when the flag is missing.
On sign-out, delete the token and revoke the session on the server. Local deletion alone leaves a valid token usable if someone copied it.
Testing with mock initial values
The package provides setMockInitialValues, which replaces the platform channel with an in-memory map:
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test('reads the stored refresh token', () async {
FlutterSecureStorage.setMockInitialValues({'refresh_token': 'abc123'});
final storage = const FlutterSecureStorage();
expect(await storage.read(key: 'refresh_token'), 'abc123');
});
}Adding a biometric check with a separate package
The storage package does not prompt for a fingerprint or face. Use a package such as local_auth to ask the user, then read the token only after a successful check. Check the storage README for options that bind items to biometrics on each platform.
Web and desktop considerations
On the web, the package stores data in browser storage with an encryption layer. This is weaker than the iOS Keychain or Android Keystore, so avoid high-value secrets there. Desktop platforms use their own system stores, and setup varies. Linux, for example, needs libsecret. Confirm the requirements in the current README.
Limits of on-device secure storage
Secure storage keeps secrets away from other apps and casual file access. It does not protect against a rooted or jailbroken device, malware running inside your app, or a user who unlocks the phone for someone else. Keep tokens short-lived, and rely on server-side revocation.
Exercise
What does readTokenSafely return when the stored data is unreadable, and what should the app do next?
Show answer
It returns null after clearing the store. The app should treat the user as signed out and ask them to sign in again.