DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Flutter databases – SQLite / sqflite, Moor, hive & ObjectBox
ObjectBox.io for ObjectBox

Posted on • Updated on

Flutter databases – SQLite / sqflite, Moor, hive & ObjectBox

As Flutter is gaining traction, the need for Flutter databases is also increasing. With more than one in eight of new apps in the Play Store being built with Flutter and 200k+ Flutter apps in the Play Store alone, the continued growth of Flutter is evident. At the same time, new requirements and end user expectations are driving a need for Edge Computing and therefore local on-device data persistence, meaning Flutter apps need a local Flutter database, a so-called edge database, more and more.

In this article we will have a look at the solutions currently available, comparing their performance. Flutter is a UI toolkit created by Google for making web and mobile apps. These are built with Dart - an object-oriented programming language and another Google’s product.

Briefly about data persistence

The databases available for Flutter / Dart only form a small subset of the huge overall database market. To make sure we’re all starting on the same page, let’s quickly review the topic of databases before diving into the Flutter database options.

What is a database?

A database is a software for storing data long-term (data persistence) as opposed to short-term caching. This enables the systematic use of digital information. Data is stored continually unless deliberately deleted, in contrast to simple caching, where data is discarded regularly. Developers can manipulate the data stored in a database in different ways via an API: store, access, search, update, query, etc. Since these operations are done in the background, end users usually don’t see them. A large number of applications need to have a database in their technology stack. The most vital database operations are abbreviated as CRUD, which stands for Create, Read, Update, Delete.

What are the main types of databases?

Databases can be categorized in a number of different ways. For the purposes of this review, the most important distinctions are between:

  • non-relational (NoSQL) versus relational databases (SQL);
  • cloud databases versus edge databases;
  • embedded versus in-memory databases. Other criteria that databases can be distinguished by include supported data types and the way they scale – definitions can vary.

What is an ORM?

Object relational Mapper (ORM) is a term that is often misused. It is not a database, but a layer that sits on top of it and makes the database easier to use. Typically, this is especially relevant for relational databases (SQL) when used with an object-oriented programming language like Dart.

Flutter database options: SQLite, hive, ObjectBox, and Moor

The amount of databases available in the Flutter Dart data persistence landscape is still very limited. These are: Firebase Realtime DB, Hive, ObjectBox, sqflite, Moor. Please note that even though Moor is not a database, we included this ORM in our overview because the number of players on the market is so small that it is worth considering Moor as a separate player.

Shortly about the current Flutter database options:

  • hive is a lightweight key-value database written in Dart for Flutter applications, inspired by Bitcask.
  • Moor (now called Drift) is a reactive persistence library for Flutter and Dart, built on top of sqlite.
  • ObjectBox Flutter DB is a high-performance lightweight NoSQL database that stores objects.
  • sqflite is an SQLite plugin for Flutter, which is a relational database without direct support for Dart objects.
  • Firebase Realtime Database is a cloud-hosted database that stores data as JSON and synchronizes it with connected clients.

What is the best Flutter database?

The answer is different for everyone. As a starting point, take a look at the following comparison matrix to make up your own mind. Note: With such a small number of options, the following overview can be a bit like comparing apples 🍎 and pears🍐.

Data persistence solution Description Primary Model Location of data Language License
hive Light key-value DB for Flutter NoSQL local Dart Apache 2.0
Moor (now Drift) ORM for SQLite used on top of a relational DB local Dart SQLite is public domain, Moor lib is MIT
ObjectBox High-performance Flutter DB NoSQL local, self-hosted server / cloud Dart Bindings are Apache 2.0
sqflite SQLite plugin for Flutter relational local SQL SQLite is public domain, sqflite lib is MIT
Firebase Realtime Database Mobile Backend as a Service (MBaaS) NoSQL Google Cloud Dart Proprietary

Flutter Database performance benchmarks

As with any benchmark, it is best to dive into the details. Accurate results are very important for us and we take benchmarking very seriously. Our benchmarking code is always open source and we encourage you to check it out. Please let us know if you note anything that does not even out in your opinion. We have been updating and improving our benchmarks for a long time 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.

CRUD Performance Results

Image description

Flutter Dart Database Benchmarks

Hive and ObjectBox clearly outperform sqflite across all CRUD operations. For create and update operations, the results show ObjectBox performing up to 70 times faster. When comparing Hive and ObjectBox, we can see the results varying 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.

Moor was not part of the benchmarking as it is an ORM. However, it is very likely it will perform similarly to sqflite, reflecting primarily the performance of SQLite.
Since Firebase is cloud-based, it is not really comparable with others. Response times of an edge database are typically always shorter when compared to those of cloud-based solutions. This is of course not the only factor involved and cloud-based solutions have their own advantages. There are some reasons which would make you choose Firebase over an edge database. Depending on the use case, it might still be a good option for you.

The Flutter database landscape - Conclusion

With Flutter growing so rapidly as a developer platform, the need for data persistence solutions is also growing. On the market today, there are only a few database options available for the Flutter community and it will be interesting to see where this will be going in 2022. If you want to learn more about databases, a good place to start is DB-engines and the database of databases. Apart from that, check out ObjectBox Dart for Flutter / Dart and tell us what you think.

Top comments (0)

All DEV content is created by the community!

Hey, if you're landing here for the first time, you should know that this website is a global community of folks who blog about their experiences to help folks like you out.

Sign up now if you're curious. It's free!