Flutter is one of the most popular cross-platform mobile frameworks used by developers worldwide according to Statista, 2021. While the study also determined that the majority of mobile developers still used native tools, Flutter is becoming a serious developer platform, and with its growth there is a growing need for Flutter databases.
A quick note on Flutter and Dart: Flutter is an open-source UI software development kit created by Google. Dart is the programming language in which developers code Flutter apps. Dart is an object-oriented programming language.
Flutter databases / Flutter Dart data persistence
While the database market is huge and dynamic, there are only few options to choose from if you are a Flutter / Dart app developer. Before we dive into the Flutter database options, advantages and disadvantages, we’re taking a very quick look at databases to make sure, we share a common ground.
What is a database?
A database is a piece of software that allows the storage and systematic use of digital information, in other words: data persistence. As opposed to mere caching, data is reliably stored and available to work with unless actively deleted. A database typically allows developers to store, access, search, update, query, and otherwise manipulate data in the database via a developer language or API. These types of operations are done within an application, in the background, typically hidden from end users. Many applications need a database as part of their technology stack. The most typical database operations are CRUD: Create, Read, Update, Delete.
What are the major types of databases?
There are many types of databases. For our purpose, the most important differentiations are non-relational (NoSQL) versus relational databases (SQL), cloud databases versus edge databases, and maybe embedded versus in-memory. However, databases can be further distinguished by additional criteria e.g. the data types they support, or the way they scale – and definitions can vary.
What is an ORM?
An Object relational Mapper (ORM) is not a database. We’re bringing this up mainly, because we see it confused often. It is a layer that sits on top of a database and makes it easier to use. This is typically especially relevant when the database is a relational database (SQL) and the programming language used is object-oriented. As noted above, Dart is an object-oriuented programming language.
The Flutter Dart data persistence landscape
At this point in time, the database landscape for Flutter Dart is still very limited. So, let us quickly introduce the current market players. Note: We are adding in Moor, because with that few player it is just one more option available and therefore, at this moment in time, should be part of the Flutter Dart data persistence landscapes, in our minds.
- Firebase Realtime DB is a cloud-hosted database. It stores data as JSON and synchronizes it to connected clients.
- Hive is a lightweight key-value database written in Dart for Flutter applications, inspired by Bitcask.
- ObjectBox DB is a highly performant lightweight NoSQL database with an integrated Data Sync. It stores objects.
- sqflite is a wrapper around SQLite, which is a relational database without direct support for Dart objects.
- Moor is a reactive persistence library for Flutter and Dart, built ontop of sqlite.
What is the best Flutter Dart database?
This of course depends… Make up your own mind with the following comparison matrix as a starting point. Note: With very few options to choose from, the following overview is sometimes a bit comparing apples🍎 and pears🍐.
|Data persistence||Description||Primary Model||Location of data||Language||License||Fun Fact|
|Firebase Realtime Database||Mobile Backend as a Service (MBaaS)||NoSQL||Google Cloud||Dart||Proprietary||acquired by Google in 2014|
|hive||Light key-value DB for Flutter||NoSQL||local||Dart||Apache 2.0||Munich brew|
|ObjectBox||High-performance Flutter DB||NoSQL||local, self-hosted server / cloud||Dart||Bindings are Apache 2.0||Munich-brew out-of-the-box data sync solution|
|sqflite||SQLite plugin for Flutter||relational||local||SQL||SQLite is public domain, sqflite lib is MIT||good old SQLite|
|Moor||ORM for SQLite||used on top of a relational DB||local||Dart||SQLite is public domain, Moor lib is MIT||Room spelled backwards|
Flutter Database performance benchmarks
As with any benchmark, you need to take a look at the details. We take benchmarking very serious and strive to get accurate results. Therefore, we also always open source the benchmarking code and encourage you to check it out. If you note anything that does not even out in your oppinion, do let us know. We have a long history of updating and improving our benchmarks continually and are happy to take any recommendations.
Performance Benchmark Test Setup
We used an Android 10 device with a Kirin 980 CPU to run the benchmarks as a Flutter app. The app executed all operations (ops) in batches of 10.000 objects. Each batch formed a single transaction. We ran each test 50 times. The results you see in the diagram are averages across all runs. We set it up that way to ensure that neither the Virtual Machine warmup during the first run nor the garbage collections affect the overall result significantly.
Flutter Databases CRUD Performance Results
Summary of the Flutter Dart DB Benchmarks
Hive and ObjectBox clearly outperform sqflite across all CRUD operations. The results show ObjectBox performing with up to 70 times the speedup for create and update operations. With regards to comparing Hive and ObjectBox, the results vary more. Hive can be faster at reading objects than ObjectBox. However, strictly speaking it’s not a fair comparison, because in Hive, the high read numbers result from Dart objects already cached in memory. If the objects are fetched using the async API from disk, the numbers drop by factor 1000.
As a cloud-based online database, Firebase is not really comparable. Local data persistence, an edge database, will typically always beat a cloud-based solutions with regards to response times. But of course cloud-based solutions have their own advantages and there may be reasons why you would choose to use Firebase over an edge database. It still may be a great option for you, depending on the use case.
Moor was not part of the benchmarking as it is an ORM. However, it is very likely it will perform similarly as sqflite, reflecting primarily the performance of SQLite.
Flutter and Data persistence landscape Conclusion
Flutter is becoming a serious developer platform and developers need a data persistence solution. There are currently only few databases supporting the Flutter community and 2022 will be an interesting year to watch where this is going. If you are interested to learn more about the database space, DB-engines and the database of databases are great starting points. Otherwise, go, check out ObjectBox Dart for Flutter Dart and share your thoughts with us – your feedback counts 🙂