top of page

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

  1. Convert instance members to json format

  2. Put the json object in the argument (arg) of the compute method

  3. 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 All

[Flutter/Dart] Format string with TextField

What 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 display it in 3-digit delimiters with a ¥ prefix. Rather than ha

Comments


Let's do our best with our partner:​ ChatReminder

iphone6.5p2.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Let's do our best with our partner:​ ChatReminder

納品:iPhone6.5①.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Theme diary: Decide the theme and record for each genre

It is a diary application that allows you to post and record with themes and sub-themes for each genre.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png
bottom of page