In this blog we will learn about how to use Flutter Secure Storage Database in Flutter.
Introduction:
Flutter secure storage is a Flutter plugin used to securely store sensitive data like tokens, passwords, or other credentials.
It provides a secure and platform-independent way to access and store data in a key-value format.
This plugin is ideal for managing small amounts of sensitive data, offering easy methods to write, read, and delete encrypted information.
Though not a replacement for full databases, it complements solutions like Hive
or SQLite
, ensuring secure data storage for modern applications.
Implementation
Setting Up Your Flutter Project:
Open pubspec.yaml
and add the following dependencies under dependencies
and dev_dependencies
.
- Add Dependencies:
1 2 |
dependencies: flutter_secure_storage: ^8.0.0 |
Run the following command to get the packages:
1 |
flutter pub get |
Creating Flutter Secure Storage Database:
This Flutter app demonstrates secure storage using the flutter_secure_storage package. It allows users to save, read, and delete sensitive data (like username and tokens) securely, with Snackbar feedback for each action.
Below is a complete example that demonstrates all the basic operations:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import 'package:flutter/material.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: SecureStorageExample(), ); } } class SecureStorageExample extends StatefulWidget { @override _SecureStorageExampleState createState() => _SecureStorageExampleState(); } class _SecureStorageExampleState extends State<SecureStorageExample> { final FlutterSecureStorage storage = FlutterSecureStorage(); String? username; Future<void> saveData() async { await storage.write(key: 'username', value: 'flutter_user'); await storage.write(key: 'token', value: 'secure_token_123'); } Future<void> readData() async { String? name = await storage.read(key: 'username'); setState(() { username = name; }); } Future<void> deleteData() async { await storage.delete(key: 'username'); setState(() { username = null; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Flutter Secure Storage')), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Stored Username: ${username ?? "No data"}'), SizedBox(height: 20), ElevatedButton( onPressed: saveData, child: Text('Save Data'), ), ElevatedButton( onPressed: readData, child: Text('Read Data'), ), ElevatedButton( onPressed: deleteData, child: Text('Delete Data'), ), ], ), ), ); } } |
Output:
Flutter Secure Storage Database in Flutter
Flutter secure storage is a Flutter plugin used to securely store sensitive data like tokens, passwords, or other credentials.
Conclusion:
Flutter secure storage is a powerful package that simplifies secure data storage in Flutter applications.
With its cross-platform support and encryption capabilities, it is a must-have for any app that deals with sensitive user data.
By following the examples and best practices in this guide, you can integrate secure storage into your app confidently.
And thanks for reading this blog for related data click here.
You can read more interesting blogs by mobikul .