How to make "compute()" function insert data to sqlite while in isolated process?


I’m working on flutter app that uses php apis for server and sqlite for local data.
The problem is with "compute()".
Here is the explanation :

I have three functions that receives data from api on the server, then add the data to my local database (sqlite) table.

First function to get data from server.

Future<List<Map<String, dynamic>>> getServerData(int vers)async {
//my code

Second function to insert data into local database:

Future<int> addNewData(List<Map<String, dynamic>>)async {
 //my code

Third function to call the first and second function:

Future<bool> checkServerData(int vers)async {
 List<Map<String, dynamic>> sdt= await getServerData(vers);
 int res=await addNewData(sdt);
 if(res>0) return true;
 else return false;

I want to call the third function in a compute function:

compute(checkServerData, 2);

When did that I found this error:
null check operator used on null value.

If I used it without calling local database it works good.
The error appears if I called the database to insert data into.

When I searched about this issue I found that it’s not allowed to access any resources which generated in one thread from another thread. But I didn’t understand exactly how to resolve it or how to use another way that do the same idea.


After searching about the issue specified, I found those workaround solutions:

1: if the process is very important to work in background, you can use the Isolate package classes and functions which allow all isolated processes or contexts to share data between them as messages sending and receiving. But it’s something complex for beginners in flutter and dart to understand these things, except those who know about threading in another environments.

To now more about that I will list here some links:
Those for flutter and pub documentation:

This is an example in website:

2: the second solution if the process isn’t important to work on background:

using the traditional approaches such as Future.builder or async/await.
You can know more about them here:

and you can review this question and answers in When should I use a FutureBuilder?

Answered By – alnajm

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.