Saving Data in Flutter

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

Part 1: Store Data with Flutter

01. Storing Options in Flutter

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Next episode: 02. Choosing a Storage Tool
Transcript: 01. Storing Options in Flutter

Data is king: the way you store and manage data can determine the success of your apps.

The good news is that in Flutter you have several options to store data locally, and choosing the right solution can enhance your app’s functionality and performance, and in the end, its success.

The purpose of this course is exactly that: help you understand and choose the best storage option for your apps, and we’ll cover this in the first part of the course, and then show how to actually use some of these in your projects.

So, let’s begin by exploring some of the tools you can leverage when storing data locally in Flutter:

SharedPreferences is a lightweight, key-value storage solution, and it’s probably one of the most common ways to store data in Flutter. With SharedPreferences you can store simple data types, like strings, numbers and booleans. Possible use cases for SharedPreferences include storing user preferences, like their favorite products, or the recently viewed content; app settings like the user’s language, or theme, or the high score in a game. But actually, you could also store Dart objects, after transforming them into strings. While SharedPreferences is not designed for complexity or large amounts of data, it’s flexible and very easy to use, so it is great for small amounts of data that you can access quickly and easily.

When security is critical for your app, Secure Storage comes into play! It’s a plugin that provides an encrypted key-value store. You could use it for sensitive information, like user credentials, API keys, credit card information or sensitive personal details. Like SharedPreferences, this is not meant to be used for large amounts of data.

Sometimes, your app may need to store images, audio files, JSON content or complex data structures. In these cases, working with local files can be a great solution. With Flutter and its APIs you can interact with the device’s file system, and read, write, and edit files stored locally.

This approach is very flexible, as you can organize your files in any way you see fit.

For more advanced storage scenarios, you can use a local database. And here you must choose between two options: SQL and NoSQL databases.

You can consider using a SQL database, like SQLite, for complex data structures and relationships. It’s also great if you already know and use the SQL language, even if it’s not required at all since most SQL options in Flutter, including sqflite, also has several helper methods that make the use of SQL totally optional.

NoSQL databases, like Sembast, or Hive, are also a very common choice when developing apps in Flutter, as they offer a flexible approach to data storage and also provide high performance.

And even if it’s not technically local Storage, as it stores data in the cloud, I think it’s also worth mentioning the Firebase Firestore database: when you enable offline data persistence, the Cloud Firestore data will be copied in your device, and users will be able to use their data even when the device is offline. When the device gets back online, your data will be synchronized to the Cloud.

In this course, after an introduction to some of the most important local storage solutions in Flutter, we will create an app that uses SharedPreferences, Secure Storage and manages local files: using these storage options in a realistic app should give you the tools to use the others as well.

But first, let’s see how to choose the best storage tool for your app next!