0
0
Fluttermobile~5 mins

Secure storage for credentials in Flutter

Choose your learning style9 modes available
Introduction

We use secure storage to keep passwords and sensitive info safe on your phone. It stops others from easily reading your private data.

Saving a user's login password so they don't have to type it every time.
Storing an API token securely after a user logs in.
Keeping secret keys safe for apps that connect to online services.
Remembering user preferences that should not be visible to other apps.
Syntax
Flutter
final storage = FlutterSecureStorage();

// Write data
await storage.write(key: 'username', value: 'user123');

// Read data
String? username = await storage.read(key: 'username');

// Delete data
await storage.delete(key: 'username');
Use FlutterSecureStorage from the flutter_secure_storage package.
All methods are asynchronous, so use await to wait for them.
Examples
This saves a token securely with the key 'token'.
Flutter
final storage = FlutterSecureStorage();
await storage.write(key: 'token', value: 'abc123');
This reads the saved token. It returns null if no value is found.
Flutter
String? token = await storage.read(key: 'token');
This deletes the saved token from secure storage.
Flutter
await storage.delete(key: 'token');
Sample App

This app shows how to save, read, and delete a username securely. Press 'Save Username' to store it, and 'Delete Username' to remove it. The saved username is shown on screen.

Flutter
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  final storage = FlutterSecureStorage();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Secure Storage Example')),
        body: Center(
          child: FutureBuilder<String?>(
            future: storage.read(key: 'username'),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting) {
                return const CircularProgressIndicator();
              }
              final username = snapshot.data ?? 'No username saved';
              return Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text('Saved username: $username'),
                  const SizedBox(height: 20),
                  ElevatedButton(
                    onPressed: () async {
                      await storage.write(key: 'username', value: 'flutterUser');
                      (context as Element).markNeedsBuild();
                    },
                    child: const Text('Save Username'),
                  ),
                  ElevatedButton(
                    onPressed: () async {
                      await storage.delete(key: 'username');
                      (context as Element).markNeedsBuild();
                    },
                    child: const Text('Delete Username'),
                  ),
                ],
              );
            },
          ),
        ),
      ),
    );
  }
}
OutputSuccess
Important Notes

Always keep sensitive data like passwords in secure storage, not plain text storage.

Secure storage uses platform-specific encryption to protect your data.

Remember to add flutter_secure_storage to your pubspec.yaml and run flutter pub get.

Summary

Secure storage keeps sensitive info safe on the device.

Use FlutterSecureStorage to write, read, and delete data securely.

All operations are asynchronous and should be awaited.