How to use Flutter secured storage

You might have some key-value pair data that you might need to keep away from malicious parties to keep your user data secured, the flutter_secured_storage is what you use to do that.

If you’re an Android developer and worked with native Android apps, you might know about EncryptedSharedPreferences It is pretty similar to the Flutter secured storage package; in fact, it uses encrypted preferences behind the scenes on Android.

Here is a quick guide on how you can use it in your project.

Install the package

flutter pub add flutter_secure_storage

then we need to initialize the package so that it can generate the encryption keys and the rest of the parameters to secure the data that you save later.

AndroidOptions _androidOptions() => const AndroidOptions(
        encryptedSharedPreferences: true,
);

Here we created a local variable that creates a new AndroidOptions object which will initialize the underlying EncryptedSharedPreferences which will be used to secure data on the Android side.

IOSOptions({
    String? groupId,
    String? accountName = AppleOptions.defaultAccountName,
    KeychainAccessibility accessibility = KeychainAccessibility.unlocked,
    bool synchronizable = false,
  })

The iOS options have parameters such as groupId which will enable sharing between widgets and other apps under the same groupId. You don’t have to use this unless you have a use case for it.

  final storage = FlutterSecureStorage(aOptions: _androidOptions());

Now we initialize the secure storage package and reference it to a variable called storage which we can use to perform save and get operations.

Read Values

To read values from the secure storage

String value = await storage.read(key: key);

The key must be a String but the return value is dynamic so you can use String, bool, int, or double with it.

Write a value (Store a value)

To write values to secured storage

await storage.write(key: key, value: value);

The key must be a String but the return value is dynamic so you can use String, bool, int, or double with it.

Delete an item

If you ever want to remove an entry from the secured storage you can use

await storage.delete(key: key);

Pass in the key of the element and it will be deleted from the flutter_secure_storage.

Read the whole data as a Map

If you want to export the data or read the whole secured storage you can use the method readAll as shown below

Map<String, dynamic> allValues = await storage.readAll();

This will return all the values in the secure storage which is encrypted into readable data which you can export or use in any way you want.

Delete the secure storage

You can delete the whole data by just calling the below method

await storage.deleteAll();

This will delete all the key-value pairs from the database.


I hope this helped you, if you have any issues comment down below.

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

You May Also Like