Saving Data in Flutter

Jan 31 2024 · Dart 3, Flutter 3.10, Visual Studio Code

Part 1: Store Data with Flutter

02. Choosing a Storage Tool

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 01. Storing Options in Flutter Next episode: 03. Challenge: Choose the Right Storage Solution

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 02. Choosing a Storage Tool

With Flutter you have many storage options, so many in fact that choosing the right one for your specific needs may look challenging at first.

The good news is that if you know what your app will do, probably the right tool is relatively easy to choose. In fact, even if tools and packages change over time, there are some universal questions you need to answer that will guide you in your choice.

The first one is: which Platform are you targeting?

Not all tools are available for all platforms, so if you are creating an app for the web, for instance, at this time you cannot choose sqflite as a storage solution. The good news is that if you get to pub.dev, you can immediately see which compatible platforms are available for your package.

Another important question is: how much data are you storing? Do you plan to store a few values or gigabytes of data? For example, sharedPreferences and Secure storage are meant to store relatively small amounts of data, so if you plan to save tons of data, they are not the right choice.

Next question you might ask yourself is: which kind of data do you plan to deal with? And in fact this question could be split in two parts: how complex your data structure is
And which data types you want to store?

In general, for complex queries, with several entities, a SQL solution might be the right choice: in this case two common plugins are SQFlite and Floor.
Sqflite is a Flutter plugin for SQLite databases. With SQFLite you can write SQL queries or use its helper methods.

Floor is built on top of sqflite. Its purpose is to simplify the interaction between the database and your dart code: It reduces boilerplate code, improves type safety and makes the code easier to read and more maintainable. On the other hand, you loose some of the control that writing SQL queries can give you.

For data that doesn’t require complex queries, is fast, and can contain large amounts of data, a NoSQL database might be a great option: examples of common NoSQL local databases you can use in Flutter include

Hive Sembast Drift ObjectBox

If you only need to store small amounts of data, one of the simplest solutions is SharedPreferences, and later in this course you’ll see how to use it in your apps, together with SecureStorage, which deals with small amounts of data as well, but also encrypts your data.

As for the kind of data you want to store, most storage options allow you to save simple data, like strings and numbers, but what if you want to save more complex items, like pictures, music or clips? In this case you need to check whether the tool you want to use supports BLOBS, or Binary Large Objects.

If your focus is editing files, you also have the option to interact with your device’s file system: you’ll see an example of that later in this course.

Then there’s one last question you need to ask: what happens when users uninstall your app? If you want to keep user data when they delete your app, of when they change their device, you need to backup your data into some cloud space, or you need to find a storage solution that does this for you: in a typical example is Firebase, which is a cloud storage solution that integrates perfectly with Flutter.

One last important point. Your local storage options are not mutually exclusive: this means that in a single app, you can choose more than one tool at the same time: a typical example would be using both the file system and a database, or a local solution with a backup on the cloud, or using secure storage for sensitive information and another tool for the bulk of your data.

OK, now that you have a better idea of your choices for storing data with Flutter, let’s get to our first challenge: will you find the right solution for a few real world scenarios?