Sub topic 12
Web and desktop considerations
This chapter assumes that you know basic Dart, async and await, Flutter package management, and authentication tokens. It also assumes that your Flutter app already builds on at least one target platform.
flutter_secure_storage is most useful when the same Flutter code must store small secrets on mobile, web, or desktop. The package keeps one Dart API, but its security properties depend on the platform underneath it.
The backend changes by platform
On iOS, the package uses the Keychain. On Android, it uses Android secure-storage mechanisms backed by the Keystore. Desktop platforms use their operating-system credential facilities when supported. The web implementation uses browser cryptography and browser-managed storage.
This gives you a consistent API, not identical behavior. A value written on Android will not automatically appear in a browser or macOS installation. Each platform has its own application identity, storage area, and lifecycle.
Web is a different threat model
A web app runs inside a browser. The browser may protect stored values from other websites, but the application still runs in a JavaScript-controlled environment.
Use HTTPS in production. The web implementation relies on browser security features that require a secure origin. Test the deployed application through its real HTTPS address, not only through a local development server.
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
class SessionStore {
SessionStore([FlutterSecureStorage? storage])
: _storage = storage ?? const FlutterSecureStorage();
final FlutterSecureStorage _storage;
Future<void> saveRefreshToken(String token) {
return _storage.write(key: 'refresh_token', value: token);
}
Future<String?> readRefreshToken() {
return _storage.read(key: 'refresh_token');
}
Future<void> signOut() {
return _storage.delete(key: 'refresh_token');
}
}Secure storage does not protect a token from malicious code running inside your application. A cross-site scripting vulnerability, compromised dependency, or unsafe HTML integration could let that code call your storage wrapper. Keep web dependencies updated, avoid injecting untrusted HTML, and use short-lived access tokens.
Browser users can also clear site data, use private browsing, switch browser profiles, or block storage. Your application must treat a missing token as a normal signed-out state.
Desktop behavior needs explicit testing
Desktop users can uninstall applications, restore system backups, or run several user accounts on one computer. Credential stores may behave differently across operating-system versions and packaging formats.
Test these cases on every desktop target you support:
- First launch with no stored value.
- Reading a value after restarting the application.
- Sign-out followed by a new sign-in.
- An operating-system account with no available credential service.
- Application uninstall and reinstall.
- A corrupted or unreadable stored value.
Do not assume that a desktop installation shares data with your mobile application. If users need to move a session between devices, use a server-side sign-in flow instead of copying the secure-storage database.
Keep platform differences behind a repository
The rest of the application should depend on methods such as readRefreshToken and signOut, rather than checking whether it is running on web, Android, or desktop. This keeps platform decisions in one place and makes tests easier.
Exercise
Why should a web Flutter app still handle a missing refresh token even when it uses fluttersecurestorage?
Show answer
The browser can clear site data, use private storage, or deny storage access. A missing token is therefore a normal state, not proof that the app is broken.