Flutter Database options are still limited. We compare the available alternatives, and share performance benchmarks.
How to persist data in Flutter / Dart?
The database market is a long-established saturated market and still experiencing double-digit growth. Most of that growth stems from NoSQL databases and newer database technologies, like time-series databases or graph databases. As Computing is shifting towards Decentralized Computing on the Edge, local databases that support decentralized data flows on Mobile, IoT, and other Embedded Devices come into focus. Some come from the Flutter data persistence world, and we will take a look at them in a second.
Before we dive into the Flutter database options and compare them, we’re quickly carifying the term to make sure we share a common ground. Don’t worry, we’ll not get theoretical, but simply make sure we share a common language.
What is a database?
A database is a piece of software to store and systematically use digital information (data), in other words: data persistence. As opposed to caching, data is reliably stored (persisted) and available to work with unless actively deleted. A database enables developers to store, access, manipulate, or delete data in the database via a developer language or API. These types of operations are done within an application, in the background, hidden from end users. The most typical database operations are CRUD: Create, Read, Update, Delete.
What are the major types of databases?
What is a Flutter / Dart Database?
Available platform-support for relational databases or native language APIs (bindings) for NoSQL databases may be very relevant to you, but does not determine the databases type. Flutter is an open-source UI software development kit created by Google. Dart is object-oriented programming language in which developers code Flutter apps. Because Dart is an ambiguous term, the term Flutter database seems to be more useful.
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.
Flutter Databases – overview
At this time, the Flutter Database landscape is still very limited. We are adding in Moor and sqflite even though they are no databases, because with that few player it is just more options to choose from.
- 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 for storing objects in Flutter with Dart.
- 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.
Which Flutter Database to use?
As always, this of course depends… but the following comparison matrix hopefully gives you a good headstart. With few Flutter Database options to choose from, the following comparison is a bit comparing apples and pears.
|Flutter Database||One-line description||Primary Data 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 🍺|
|Moor||ORM for SQLite||used on top of a relational DB (SQLite)||local||Dart||SQLite is public domain, Moor lib is MIT||Room spelled backwards ↔️|
|ObjectBox||High-performance Flutter Database||NoSQL, object-oriented||local, self-hosted server / cloud||Dart||Apache 2.0 bindings||Munich brew 🍻 and 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 wrapped freshly 🎁|
Which Flutter Database is the fastest? Performance Benchmarks
As with any benchmark, take a look at the details yourself. We take benchmarking very serious and strive for meaningful results and open source the benchmarking code. Do check them out. If you note anything that does not even out in your mind, do let us know. We have a long history of updating and improving our benchmarks continually and are happy to take any recommendations.
Flutter Database 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 speed 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.
Firebase, as a cloud-based online database, is not comparable. Persisting data locally with an Edge Database will always beat a cloud-based solutions with regards to response rates / speed. Of course cloud databases have their own advantages and there may be reasons why you would choose to use Firebase over an Edge Database. A cloud database like Firebase may be a great option for you, depending on your use case.
Moor was not part of the benchmarking as it is an ORM similar to sqflite as it is likely it will perform similarly as sqflite, reflecting primarily the performance of SQLite.
Flutter Databases – Summary and Conclusion
Flutter is becoming a serious developer platform and Dart developers need a data persistence solution. There are currently only few databases supporting the Flutter / Dart community. 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 DB for Flutter Dart and share your thoughts with us – it’s not too late yet; we are shortly before releasing an updated version – and your feedback counts 🙂