[Flutter/dart] How to call a method that takes a class instance as an argument in a compute() method
Overview
If heavy processing is applied while creating an application with Flutter, the operation of the application will become heavy. In such a case, you can lighten the operation by executing the process in another thread (isolate). The compute function makes it easy to execute processing with another isolate like this. (Click here for details)
var result=await compute(func, arg);
//func is heavy process、arg is the argument of func
However, only one argument of func can be specified, and arg can only be a variable of a specific type such as int and String.
However, in my app, there was a heavy process that took a class instance as an argument. Therefore, I will introduce how to deal with such cases.
Method
In the first place, you may have wondered, "Should I make the process an instance method and assign it to func?" (I thought so at first), but another isolate can only execute static methods. If you try to execute instance method,
Illegal argument in isolate message : (object is a closure - Function '<***>':.)
occurs.
Returning to the main subject, you can pass a class instance as an argument by the following method.
Convert instance members to json format
Put the json object in the argument (arg) of the compute method
Create an instance from json in process (func)
Let's take a closer look below.
1. First, let's prepare a method to convert member variables to json in the target class. We will also define a constructor that creates an instance from json, which will be used later.
class Sample{
String _content;
int _number;
//convert to json
Map toJson()=>{
'content': _content,
'number': _number,
}
//make an instance from json
Sample.fromJson(Map json){
_content=json['content'];
_number=json['number'];
}
2. Let's pass the json object as an argument to the compute function
Sample _sample;
//methods such as creating the _sample instance
Map json=_sample.toJson();
var result=await compute(heavy_method, json);
//heavy function
static Future<bool> heavy_method(Map json) async{
//some heavy process
}
3. Let's create an instance of the Sapmle class from json at the beginning of heavy_method.
//heavy function
static Future<bool> heavy_method(Map json) async{
Sample _sample=Sample.fromJson(json);
//some heavy process
}
Lastly
This method takes time to convert to json and re-instantiate. Also, it seems that there is time to switch to another isolate with the compute function. Please consider this method with that in mind. (Reference)
In my case, the processing weight was overwhelmingly dominant, so I used this method.
(In the first place, if I were told not to make such a process, ...)
Recent Posts
See AllWhat want to do I want to create an input form using TextField. For example, if the input content is a monetary amount, I would like to...
What want to do There is a variable that remain unchanged once the initial value is determined. However, it cannot be determined yet when...
What want to do As the title suggests. Place two widgets in one line on the screen One in the center of the screen and the other on the...
Comments