Michał Przybylski
Business Development Manager
Magdalena Jackiewicz
Reviewed by a tech expert

How to offload an app with a large database in Flutter?

Read this articles in:

Sometimes your local database contains a huge amount of data and you just need to deal with it. Such read-heavy applications lead to significant slow down, screen freezing and other nightmare scenarios. Simple applications with GUI work mostly on a single thread, knowns as the ”main” thread, but when the app grows, more and more tasks need to be processed. That usually forces developers to move some methods onto a different thread – just like when an application has to work with heavy computations, e.g. processing queries in huge databases.In this article, I will demonstrate how to avoid screen freezing of read-heavy apps in Flutter using the Drift framework. The method allows to manage large databases without compromizing the performance, especially in UI. Let’s see how it’s done in three simple steps.

Step 1: Enable DriftIsolate

To work with threads, Flutter uses a class named Isolate. In Drift, there is a special class named DriftIsolate, which makes the transfer of your database onto another thread much easier.

Let’s start by enabling this feature. Create a file named build.yaml in your project root directory (not in lib\ folder!). Include the below code:

After that, run the build with build_runner to generate a new constructor for your database class, which will allow allow you to open the database in another thread:

Step 2: Open the database in another Isolate

When your database class is ready, the next step is to open it on another Isolate:

In the _backgroundConnection method, the first two lines will create your in-memory database and then create a connection between that database and the executor. But, dealing with an existing database in a file using background isolate is more difficult. I will show how to simplify it in the following section.

Step 3: Initialize with path_provider

Currently Flutter’s path_provider is not available for use on background Isolates. Some methods for providing the path (e.g. getLibraryDirectory() ) have to be called on the main thread. To solve this problem you can calculate the path first, then send it to DriftIsolate:

By using DatabaseConnection.delayed() asynchronous initialization of database can be done synchronously.

We’re done here!

After implementing this solution, you will notice that your app will work much faster while it processes database queries. It also resolves the screen freeze problem, because all heavy computation is away from the main thread. Let me know if this article was helpful for you.

People also ask

No items found.
Want more posts from the author?
Read more

Want to read more?

CTO Corner

Introduction to data-centric AI

Explore the fundamentals of data-centric AI. Unlock insights into harnessing data for powerful AI applications.
CTO Corner

Introduction to Data Lakes – how to deploy them in the cloud?

Get started with data lakes in the cloud. Learn deployment strategies and harness the power of data for your organization.
CTO Corner

Introduction to data warehouses: use cases, design and more

Discover the benefits and use cases of data warehouses in this comprehensive introduction. Unlock data-driven insights for success.
No results found.
There are no results with this criteria. Try changing your search.