Is there a way to kill tasks created by the compute function?

Issue

I know that the compute function is based on the Isolate API. For Isolate, you can request lsolate to shut down by calling the kill method of the islate object.

Can the compute function manually close Isolate running this task?

Solution

No

It is not possible to kill a compute function.

Why

The reason for this is that looking at the source code of the compute function, the created isolate is only killed after the result completer has finished:

final Completer<R> result = Completer<R>();
...
await result.future;
...
isolate.kill(); // Always awaits the result.

The result is only completed if there is either an error or the function you pass to compute returns.


Furthermore, you do not have access to the isolate yourself because it is created inside of the compute function.

Solution

If you want to be able kill the isolate you launch, do not use compute. Instead, you will have to create the Isolate yourself.

Answered By – creativecreatorormaybenot

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.