Choose the Best Data Storage Solution for Your Flutter App

Flutter category image

Whether you need local storage, secure storage, or a cloud database, Flutter has you covered. Here is a guide to choose the best data storage solution for your Flutter app.

Depending of what type of app you develop, you might need different data storage solutions. In this article, I’ll introduce various options for Flutter developers of how to manage data storage in your apps. Afterwards, you will be able to choose the best data storage solution for your Flutter app. The following topics will be covered:

  • Local storage
  • Secure storage
  • Cloud storage

In this article, we will distinguish between two use cases for databases: storing large data blobs such as PDF files or images, and storing small key-value pairs. Whatever your needs may be, there is a solution available for you.

Local storage

Local storage means that data is only stored on the user’s device. This is useful for apps that don’t require a permanent internet connection, or perform all operations on the device itself.

Localstore is a persistent document store for many small JSON files. To store large data chunks, it is better to write directly to files.. If you need a key-value store, there is Shared Preferences or Hive available for you. Every approach presented here will store the data persistently, so that it is available again when the user launches the app.

Let’s dive into some code examples!

Localstore

Localstore is a persistent NoSQL database based on JSON files. It is suitable for smaller JSON files, but not for larger ones. In such cases, it is better to read and write the files directly, without using a database.

Easy and simple approach to handle multiple small JSON files in your app!

Reading and writing files

The most basic approach for data storage in Flutter is to read and write files. In fact, every package that uses persistent storage does this in the background, although you may not notice it.

Reading and writing files in Flutter is not difficult. However, it does require more code than using a package. On the other hand, you have more control over the process. Simply import dart:io and you’re ready to go. Here are the steps:

Yes, it is more code and you most likely want to write a wrapper in your app, but you have the maximum amount of control with this approach.

SharedPreferences

Shared Preferences is a useful tool for persisting small amounts of data, such as user settings. It’s a key-value store similar to a Dictionary in Dart. The API is easy to learn, with getter and setter methods for various types like Int, Double, Bool, String, and StringList.

I recommend storing only user or app settings in Shared Preferences. Create a class for all settings, implement a toJson() method, and use jsonEncode() to generate a string from it. Then save it with setString() and load it with getString() whenever you need it. If you are not familiar with JSON handling in Dart, have a look at this tutorial.

Here is a code example showing how to read and write data with the Shared Preferences package:

For simple use cases, Shared Preferences might be a viable solution. However, serious app developers often prefer other approaches.

Hive

Hive is a lightweight and blazing fast key-value database written in pure Dart. It works on all platforms, is simple to use, and has no native dependencies. Similar to Shared Preferences, you can use Hive as a Dictionary. However, a key-value store is called Box. The process is simple:

The first step is always to initialize Hive. This needs to be done exactly once as long as the app runs. For Flutter apps, just add the code await Hive.initFlutter(); somewhere before you access a box for the first time.

You can store any primitive data in a box. Here’s an example:

With a type adapter, you can even store custom objects in your database. The class must derive from HiveObject and the properties must be annotated. Then, you can also benefit from save() and delete() methods!

If your boxes contain a lot of data and you want lazy loading, use a LazyBox. For higher security, Hive also provides EncryptedBoxes with AES 256 CBC and PKCS7 padding.

Hive has everything that a key-value database needs to offer. If you are still unsure whether Hive is the right choice for your next project, read these hints on when to use Hive and when not to use it.


Join my free Medium Newsletter!

Get an overview of my new Medium content with my monthly newsletter. Save time and pick what you like to read!


Secure storage

To store data securely, you can use an encrypted version of Shared Preferences called Flutter Secure Storage. Alternatively, Hive also offers encrypted boxes. These work similarly to the default boxes in the previous example, but with the added benefit of secure storage at the cost of speed.

Flutter Secure Storage

Flutter Secure Storage is a useful tool for storing data securely. It is an encrypted version of Shared Preferences and allows you to store sensitive data such as user authentication tokens, API keys, and other confidential information. It provides a read and write method for any type of data, and it is easy to use with a simple and intuitive API.

This package uses the secure storage of the host system and is compatible with iOS, Android, MacOS, Linux, Windows, and the Web platform. Please refer to the documentation for instructions on how to configure the package for each platform.

Here is a code example to get you started:

Remember to add flutter_secure_storage to your pubspec.yaml file before using it.

Check out the Flutter Secure Storage documentation for more details and options.

Hive with encrypted boxes

We previously discussed Hive. One major benefit is that it also provides encrypted boxes to securely store your data.

All you need is an encryption key that Hive can generate for you. You need to store that encryption key in a safe location. The following code example uses Flutter Secure Storage, but you can also use other packages or methods if you prefer.

Let’s have a look at the code:

?

Hint

  • Only values are encrypted, keys are stored in plain text
  • If you use a wrong encryption key, you may get confusing results. There is no check if a key is correct!

Storing the encryption key in a secure location can make this solution somewhat cumbersome.


Cloud storage

When choosing the best data storage solution for your Flutter app, you should always consider cloud-based storage options. A lot of Flutter developers use Firebase as their cloud solution due to its easy integration with Flutter apps. Firebase provides a NoSQL database called Firestore to store JSON documents, as well as a Storage that acts as a file store.

Firestore

Firestore is a NoSQL document database and part of the Google Firebase services. It uses a hierarchy of collections and documents to structure data and is optimized for large collections of small documents. It is not useful, or even possible, to store entire files in it.

The Flutter package for Firestore uses well-known methods like get, update, set, and delete to manipulate data. Here is some example code on how to deal with it.

To get a quick start with Firestore, check out my detailed article below:

Level up your Firebase skills!

Check out my free email course on Firebase fundamentals, and grab your copy of my Firebase ebooks. Start building amazing apps with Firebase today!

Storage

Storage is a blob storage service and part of the Google Firebase services. It is useful when you want to store PDF, images, or text files in a cloud environment. With its Flutter package, you can easily upload and download files as you like. Have a look at the following code examples.

Get a storage reference (file pointer)

Read data

Write data

For more details about Firebase Storage, read my detailed article below:

Conclusion

In conclusion, there are various data storage solutions available for Flutter developers, depending on the type of app being developed. This article covered local storage, secure storage, and cloud storage options, as well as use cases for storing large data blobs and small key-value pairs. With the appropriate storage solution, developers can ensure that their app’s data is managed efficiently and securely.

Related articles