It’s hard to imagine an app that doesn’t have any data in it. And the data has to be stored somewhere. Depending on the app’s needs, this place can be a local database or a cloud. For example, in our recent fitness app tutorial, we used a local database to have access to the data even offline.
But how do you choose the best suiting database for your Flutter project? Let’s discuss what to pay attention to.
The easiest way to add a database to your Flutter app is with a ready-made package. Just a few lines of code and you have a database to store all of your app’s information. What are some of the most popular database packages out there? Those are
- Cloud Firestore
- Flutter secure storage
- and Shared preferences.
Let’s see what are the peculiarities of each of them and how do they suit different projects.
We are starting out with the most popular developer’s choice: SQLite. Basically, it is a universal SQL-based database.
The “lite” part is there for a reason. This database package is all about being compact and self-contained.
To use SQLite in a Flutter project you would need a sqflite package. It is the most popular and convenient way to connect this database.
Don’t forget to use the path_provider package along with the sqflite one.
While SQLite is a way to store your data locally, Cloud Firestore is a cloud-based service. It is more suitable for apps that involve a lot of real-time interactions.
Cloud Firestore is a pre-configured NoSQL database, meaning that each piece of data is stored as a separate document. As a result, you can change your data structure at any time.
Setting up Cloud Firestore might take a bit more effort as compared to the other three tools but it is definitely worth it if you want to synchronize any data quickly. For example, you might be building an app where many users exchange the data, so it should be in sync at all times.
Flutter secure storage was our choice for the fitness app we recently showed you how to make. Why? The answer is quite simple and the hint is in the name: this database helps you deal with some sensitive data.
Because the database we were making was to contain the users’ private data, such as their full names and emails, our biggest priority was to make sure that this information is stored safely.
Secure storage is a library created specifically for the Flutter apps, so it’s rather easy to use. You just need to add the flutter_secure_storage: *current version* package to your code and then work it out with your variables. We showed how to do that step by step in the tutorial.
The shared preferences package is perfect when you don’t have loads of data to store. If it’s just a number or a string, for example, the username of a current user, the shared preferences package is more than enough.
Just like the secure storage one, this package is designed specifically for Flutter. That’s why adding and running it only takes a few lines of code. The package is called shared_preferences: ^ *current version*.
As you can see, there is a database package for any situation. Not always do you need a complex cloud system; in many cases, SQLite is more than enough. Whether your data is just a bunch of usernames or tons of information to be accessed by multiple devices at a time, there is a fit for you.