Scroll down to discover

How to cache data securely in Flutter using cache_x package

January 27, 2022Category : How To Tech Talk
How to cache data securely in Flutter using cache_x package

cache_x | Flutter Package (

Caching is just a simple key-value pair data-saving procedure. Usually, for caching data locally in flutter we use the shared_preferences package in flutter. Shared Preferences package under the hood uses localStorage on the web, SharedPreferences on Android, and NSUserDefaults on iOS.

But the problem is shared_preferences package stores plain text which is not secure at all. Especially if you are storing sensitive information in localStorage it can be read by the client and it’s a problem if vulnerable to XSS. SharedPreferences and NSUserDefaults are also can be compromised on the client side since they are not secure storage and not encrypted.

cache_x package is a wrapper around shared_preferences. cache_xuses SharedPreference as storage for caching data. Since we really can’t just save the original data because of security issues. cache_xuses AES encryption & decryption behind the scene when you are caching data or fetching data from the cache.

After using cache_xthis is how the data will appear in the localStorage.

Screenshot from Example project of cache_x package
Image from Example project of cache_x | Tanvir Ahmed Khan

So now lets see how can we use cache_x package to securely store data locally.

First, we have to add the cache_x package dependency in pubspec.yml

cache_x: ^latest

Get the version from the

Then in the Main Function of our App, we have to initialize the CacheXCore with a strong password. Keep in mind the Password length must be 128/192/256 bits which means 16, 24, or 32 characters.

As the CacheXCore is initialized properly now we can use the CacheXCore to save and retrieve any data.

CacheXCore instance is Singleton so once it’s initialized you can use it anywhere in the app you want.


There are 5 methods to save 5 type of data which are:

1. saveBool(String key, bool value)

2. saveDouble(String key, double value)

3. saveInt(String key, int value)

4. saveString(String key, String value)

5. saveStringList(String key, List<String> value)

And to retrieve the data there are methods which are:

1. getBool(String key)

2. getDouble(String key)

3. getInt(String key)

4. getString(String key)

5. getStringList(String key)

6. getKeys()

So if we want to save any String Data we can call the saveString() function like this:

await CacheXCore.instance.saveString(key: 'ft1', value: 'Tanvir');

and to retrieve the String data we can call the getString() function like this:

CacheXCore.instance.getString(key: 'ft1');

Here is the full example project that shows all the methods in action:

Important things to keep in mind

1. The password must be 128/192/256 bits. It means the password only can be 16 character,24 character or 32 character in length. Otherwise it will throw exception.

2. Provide defaultValue in the get methods or else the default value will be null. If the key is not found or the data is not the right type it return the defaultValue.

3. If you forget or lost the specified password all the data encrypted with the password will be unusable.

4. All the save methods will return Future<> so to get the value you have to use await

It will throw exception if there any.

Exceptions in the Package

1. CacheXException: If there any exception in the CacheXCore or the CacheXCore is not initialized properly it will throw CacheXException exception. The exception message starts with CacheXException:.

2. StorageException: If there any exception saving or getting data it will throw StorageException. The exception message starts with String StorageException:.

3. EncryptionException: If there any exception encrypting or decrypting the data it will throw EncryptionException. The exception message will start with String EncryptionException:.

Other Projects

TransitionX, Rounded Button, CacheX

© Robust Research and Development Ltd. 2022 / All rights reserved.
To top