Differents ways to store data in Flutter and their most suitable use cases

Maxime F.
4 min readMar 20, 2022

--

In almost every application there’s always some sort of data that should be saved. It could a simple token, a date or a simple number. The purpose depends on each use case but sometimes we use the store engine that doesn’t suit the purpose rather the one whom we are the most familliar or easily integrable.

So for each use case there’s some store engine that suit the most and others that doesn’t. There is a list of some i used to use in my applications.

For local storage

SharedPreferences

SharedPreferences is probably the first that everybody think of when we have to store data in our application quickly and without having to spending too much time in integration. It is lite, simple and available in almost all platforms supported by Flutter using the package shared_preferences.

It’ll suit the most when you have to store simple data like string, number, bool or even a little serializable object. The package shared_preferences is simple but if you need some specific behaviour like you want your data to be or not to be backed up you must configure it natively through AndroidManifest for example.

KeyChain/KeyStore

If you think about saving sensitive data then think about KeyChain and KeyStore depending you are targeting iOS or Android. Data stored there are encrypted/decrypted by platform specific algorithm.

It’ll suit well for data like token (OAuth tokens, JWT Tokens…), encrypted/hashed passwords and simple data that you judge necessarly to be save securely. Take in mind that only string can be saved but with serialization/deserialization we can do anything (in the allowed limit of size).

For this depending on the case, you can use the package flutter_keychain, that’s an implementation to access these APIs through your Flutter code or you can use the package flutter_secure_storage to save data securely as it encrypt/decrypt data with a secret key and that key is saved in KeyChain/KeyStore.

I’ll recommend you flutter_secure_storage over flutter_keychain cause it is more configurable and available for more platforms.

Databases/Files

When it comes for large data like list of entities, text of books etc, the previous one could quickly comes with great disadvantages. It is at this moment that you must think of something more suitable for heavy data, maybe something that could allow to do queries and pagination. There come databases and files.

Files will be most suitable when you have to save file or data like file. It could be a huge base64 Image, a certificat or pages of books. You can achieve this using the File API of dart:io or use a package like flutter_secure_file_storage that will allow to save sensitive file.

Databases in the other hand will be most suitable for list of data, entities or any data that could be filtered. Here depending on the case you can choose a package like sembast that is good for simple data or list of data and allows you to make filter/query. If you are more familiar with SQL query or your project requires it you can choose the package sqflite that will allows you to make your SQL query. If you are more familiar with ORM, you can choose objectbox that manage entity like any others ORM and allows you to make complex request without having to know any SQL Syntax.

For remote storage

Storing data in local could do the job in almost all cases but sometimes you want that your data to be synchronised so that the user could access it on any device. In these particulars cases, remote saving will be the only option but there’s many ways.

Your RestFul API

RestFul API allows you to do request to server and this way save/access data on any device. It is most suitable for synchronisation when you have data stored in local but also in server and synchronised with others devices.

Third Party Services

Third Party services could be used to save data remotely when you don’t want to have a server and maintain it. It is mostly useful when you only want to save data remotely and nothing else like doing some computation on it. There is services like Firebase Database or Firebase Firestore that allows you to save data remotely and even allows to listen to update on data from different devices at almost the same time. If you are not a Google services fan you can AWS Amplify with their dedicated flutter packages or any others third party services.

Last words…

There’s many others packages/services that will suit well for these use cases. I just didn’t want to make a list a packages as it is not the purpose of this article.

I recommend you to not directly use each store engine APIs in different place in your code but put a new layer in between like a Data Provider or DAO (Data Access Object) that will be in charge to request either a local storage or remote storage or maybe both.

--

--

Maxime F.
Maxime F.

No responses yet