Which Flutter database should you use?
Tags: flutter, dart, database, performance
Published: 2021-11-05 13:10:11

As [Flutter](https://flutter.dev/) continues to gain prominence, so does the need for efficient and reliable data management within Flutter applications. In May 2023, there are already over [1M Flutter apps](https://techcrunch.com/2023/05/10/with-over-1m-published-apps-googles-flutter-expands-its-support-for-web-apps-and-webassembly/) published. This demand has given rise to a diverse range of Flutter database options, each offering unique features and capabilities. In this article, we delve into the landscape of Flutter databases, comparing the various options available.


## Briefly about data persistence

When it comes to databases, the ones available for Flutter/Dart are just a small part of the overall database market. Before we jump into exploring the various Flutter database options, let's quickly refresh our understanding of databases to ensure we're all on the same page.

### 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](https://en.wikipedia.org/wiki/Cache_(computing)), 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 **C**reate, **R**ead, **U**pdate, **D**elete.

### What is an ORM?
[Object relational Mapper](https://greenrobot.org/android/sqlite-access-with-an-orm-faq/) (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. 

### What kind of databases are there?
Databases can be categorized in a number of different ways. For the purposes of this review, the most important distinctions are between:
* cloud databases versus [edge databases](https://objectbox.io/what-is-an-edge-database-and-why-do-you-need-one/);
* non-relational (NoSQL) versus relational databases (SQL);
* embedded versus in-memory databases.
Other criteria that databases can be distinguished by include supported data types and scalability – definitions can vary.

## 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**](https://github.com/hivedb/hive) is a lightweight key-value database written in Dart for Flutter applications, inspired by Bitcask.
* [**Floor**](https://pinchbv.github.io/floor/) is an object-oriented SQLite database wrapper for Flutter, inspired by Room, a similar library for Android.
* [**Drift**](https://drift.simonbinder.eu/) is a reactive persistence library for Flutter and Dart, built on top of sqlite. 
* [**ObjectBox Flutter DB**](https://github.com/objectbox) is a high-performance lightweight NoSQL database that stores objects.
* [**sqflite**](https://en.wikipedia.org/wiki/SQLite) is an SQLite plugin for Flutter, which is a relational database without direct support for Dart objects. 


## What is the best Flutter database?

Determining the best Flutter database is subjective and varies for each individual. To assist you in making an informed decision, consider referring to the following comparison matrix as a starting point. However, it's important to note that due to the limited number of options available, comparing them can be akin to comparing different flavours of ice cream 🍦. Ultimately, the choice depends on your specific needs and preferences.

| Name | ORM/Database | Storage Type | Data Model | Language |
| --------- |:-------:|:-------:|:-------:|:-------:| -------:|
| **sqflite** | SQLite plugin | SQLite | relational | SQL | 
| **Floor** | ORM | SQLite | used on top of a relational DB | local |  SQL |
| **Drift** | ORM | SQLite | used on top of a relational DB | Dart or SQL | 
| **ObjectBox** | Database | File-Based | NoSQL | Dart |
| **Hive** | Database | File-based | NoSQL | Dart |


## Flutter Database performance benchmarks

As with any performance test, it is best to [run Flutter Databases benchmarks yourself](https://github.com/objectbox/objectbox-dart-performance). Accurate results are very important for us and we take benchmarking very seriously. 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
![Flutter benchmarks: sqflite vs ObjectBox vs hive](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pqhiakpzp1tbp6u6tlhm.png)

### 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.

## The Flutter database landscape - Conclusion
 
Data persistence solutions for Flutter are becoming more important since Flutter grows so quickly as a developer platform. Only a few databases are now offered to the Flutter community on the market, so it will be interesting to see how this grows in the future. Through our comprehensive comparison, we have explored key criteria such as ORM support, storage type, data model, and the programming language used. By evaluating these factors and considering individual preferences, developers can make informed decisions to optimize data management and enhance the performance of their Flutter applications. Regularly staying updated with emerging options and community feedback will help you choose the most suitable Flutter database for future projects.