Run Flutter Plugins inside Isolate Process and Get Feedback On Completion.

Lavkant Kachhwaha
speakX Product & Engineering
3 min readSep 1, 2021

Basically when we try to run Flutter plugins inside dart:Isolate process, it shows an error of Flutter Widget Bindings; and we can't run plugins with isolate processes.

So the main reason for this is that dart:Isolate fully works on dart itself and it doesn’t have a flutter dependency like Widget Bindings, UI Bindings, Flutter Life Cycle Events and most of plugins we pull from pub.dev. This is why plugins don’t work with dart:Isolates.

We have a dart:Isolate by default inside dart package so we don't need any third party plugin for that.

We have Flutter plugin flutter_isolate that contains all the flutter bindings and dependency so our plugins can work inside this isolate process and it has an callback function which we can use for await response or send response to main isolate as callback.

OUR GOAL IS TO COMPRESS SELECTED VIDEO IN BACKGROUND AND PRINT PATH OF COMPRESSED FILE IN MAIN ISOLATE

We are using video_compress that use to compress video, and that can't be run with default dart:isolate in Flutter

IsolateFunction is main function thats responsible for compression of video using video_compress plugin in Flutter and this function runs on separate Isolate.

static isolateFunction(Map<String, dynamic> context) async {
//MESSENGER SUBSCRIBE TO ISOLATE COMMUNICATION
final messenger = HandledIsolate.initialize(context);
//LISTEN FOR FILEPATH FOR COMPRESSION
messenger.listen((filePath) async {
VideoCompress.setLogLevel(0);
final info = await VideoCompress.compressVideo(
filePath,
quality: VideoQuality.MediumQuality,
deleteOrigin: false,
includeAudio: true,
);
var newSize = await (File(info.path).length());
newSize = (newSize / 1024).round();
//MESSENGER SEND BACK CALLBACK WITH COMPRESSED FILE PATH
messenger.send(info.path);
});
}

AfterCompressionUpload function is callback function that runs in Main Isolate, here we can take callback and assign any other work like uploading or cropping on compressed video.

void afterCompressionUpload(String path) {
var filePath = path;
//EDITED FILE REPLACED WITH COMPRESSED FILE

postFile.editedFile = File(filePath);
//ASSIGN POSTFILE TO MAP
map['postFile'] = postFile; //DISPOSE ISOLATE WHEN PROCESS FINISH
isolates.kill("Compressor");
print("Response on main thread : Compressed file path $path");
}

Spawning separate isolate named “Compressor” from main Isolate for Video Compression, this name is later use to kill isolate process.

onRecieve : Executed every time data is received from the spawned isolate.

onInitialized : Executed once when spawned isolate is ready for communication.

isolates.spawn<String>(   //MAIN ISOLATE FUNCTION
isolateFunction,

//NAME OF ISOLATE PROCESS Compressor
name: "Compressor",
// Executed every time data is received from the spawned isolate.
onReceive: afterCompressionUpload,
// Executed once when spawned isolate is ready for communication.
onInitialized: () => isolates.send("filepath", to:"Compressor")
);

Reference

--

--