Security

Flutter security: tokens, storage and data protection

A practical guide to Flutter app security: token and session handling, secure storage, network security and how to protect sensitive user data.

Fabrizio Billeci
Fabrizio Billeci
15 February 2026 · 5 min read
Flutter security: tokens, storage and data protection

Introduction

Mobile apps handle sensitive data: authentication tokens, credentials, personal information. Exposing them means theft risk, man-in-the-middle attacks, reverse engineering. In Flutter, security requires care for storage, network and session management.

This article covers token and session management, secure storage, network security and protection of sensitive data in a professional way.

Token and session management

Access token and refresh token

The standard approach uses an access token (short-lived, e.g. 15 minutes) and a refresh token (long-lived, e.g. 7 days). The access token is sent with every API request; when it expires, the refresh token is used to obtain a new pair without asking the user to log in again.

Interceptor with Dio

With Dio you can use an Interceptor that automatically adds the Authorization: Bearer <token> header to every request. On 401, the interceptor can call the refresh endpoint, get a new access token, save it to secure storage and retry the original request.

"One Future" pattern for concurrent refresh

If multiple requests receive 401 simultaneously, avoid triggering multiple refreshes in parallel. Use a single shared Future: the first request starts the refresh, the others wait for the same Future. This way only one refresh call is made and all requests are retried with the new token.

Logout

On logout, remove both access and refresh tokens from storage, invalidate the session server-side (if supported) and redirect to login.

Secure storage

Why not SharedPreferences

SharedPreferences stores data in plain text: anyone with device or backup access can read it. For tokens, passwords and sensitive data use flutter_secure_storage.

flutter_secure_storage

Package that uses native security APIs: Keychain on iOS, EncryptedSharedPreferences (AES + MasterKey) on Android. Data is encrypted; read/write is transparent. Also supports macOS, Windows and Linux; on web it only works over HTTPS or localhost.

final storage = FlutterSecureStorage();
await storage.write(key: 'accessToken', value: token);
String? token = await storage.read(key: 'accessToken');
await storage.delete(key: 'accessToken');

Android options (aOptions)

On Android you can configure AndroidOptions: encryptedSharedPreferences to use EncryptedSharedPreferences, resetOnError to clear data on decryption error (useful after OS updates that invalidate keys).

iOS options (iOptions)

On iOS you can set accessibility (e.g. KeychainAccessibility.first_unlock) to control when the key is accessible. Avoid options that make data accessible when the device is locked unless strictly necessary.

Network security

HTTPS mandatory

Flutter on iOS and Android blocks HTTP (cleartext) connections by default. For local dev you can allow exceptions via network_security_config.xml (Android) or Info.plist (iOS), but only for debug domains. In production always use HTTPS.

SSL Pinning

Pinning binds the app to a specific certificate or public key. Even if an attacker intercepts with a proxy (MITM), the connection fails because the presented certificate does not match the pin. Two variants:

  • Certificate pinning: pins the SSL certificate. Less flexible: every certificate renewal requires an app update.
  • Public key pinning: pins the hash of the public key (e.g. SHA-256). More resilient to certificate renewals.

Packages like http_certificate_pinning or dio_http2_adapter with SecurityContext allow setting pins. Best practice: prefer public key, consider Remote Config or server-fetched updates to refresh pins without a new release.

Android: network_security_config

To control cleartext and certificate trust, reference an XML file in AndroidManifest.xml:

<meta-data
  android:name="io.flutter.network-policy"
  android:resource="@xml/network_security_config" />

In res/xml/network_security_config.xml define domains for which cleartext is allowed (debug only) and trust policies.

Sensitive data protection

Do not hardcode secrets

API keys, secrets and passwords must not go in code. Use --dart-define or --dart-define-from-file at build time, with files excluded from git. In CI values come from a secret store. For maximum security, critical keys should be managed server-side only.

End-to-end encryption (E2EE)

For highly sensitive data (messages, health records, financial data) E2EE ensures only sender and legitimate recipient can read. Use key exchange (e.g. X25519), symmetric encryption (AES-GCM, ChaCha20-Poly1305) and private keys in secure storage. The backend stores only encrypted payloads.

Logging and debug

In release disable verbose logging and never log tokens, passwords or PII. Use build flags or environment to enable debug only in dev/staging.

Protection from reverse engineering

The compiled app can be decompiled. Do not base security on "hidden secrets" in the client. For sensitive operations (payments, critical auth) delegate to the backend and use attestation/root detection only as an additional layer, not as the sole defence.

Security checklist

  • Tokens in flutter_secure_storage, never in SharedPreferences.
  • Refresh token flow with interceptor and "One Future" pattern for concurrent requests.
  • Logout: clear tokens and invalidate session.
  • HTTPS everywhere in production; cleartext only for debug domains if needed.
  • SSL pinning (preferably public key) for apps requiring MITM protection.
  • Secrets via dart-define or secret store, never in code.
  • E2EE for highly sensitive data when the use case requires it.
  • No logging of sensitive data in release.

Conclusion

Security in Flutter requires deliberate choices on storage (flutter_secure_storage), token management (interceptor + refresh), network (HTTPS, pinning) and data protection (no hardcoding, E2EE when needed). Applying these practices reduces risk and aligns the app with industry standards and compliance requirements.

These practices sit well next to a solid environment and release setup. For a security review of your project, get in touch.

Fabrizio Billeci
Fabrizio Billeci
Flutter & full-stack app developer · Catania, Italy

Back to the blog

Related articles

Have a project in mind?

Tell me your goals and timeline: I'll get back to you with a concrete proposal, a roadmap and a cost estimate.

Let's talk about your project