Working with Dart Isolate is not very easy for beginners and tedious to write big boilerplate code for veteran and even though there are lot of packages to solve this issue none were exactly as straight forward as they try to be.
Well then EasyWorker might be the simplest way to work with dart isolate so far.
- Easy to learn and use
- Simple to start a long running process or a short computation.
- easiest bidirectional communication
dependencies:
flutter:
sdk: flutter
easy_worker:
...
For long running task and bidirectional communication between your main isolate and the worker isolate, You will need two things:
- A static or top level function for example this factorial calculator
/// This takes number (message/payload or whatever you wanna say) and a sender
void calculateFactorial(int number, Sender send) {
int temp = number;
number--;
while (number > 0) {
temp *= number;
number--;
// just to simulate long running task with blocking operation
// (operation that can freeze the ui thread)
sleep(const Duration(seconds: 1));
}
/// once done send the calculated result to the parent process
send(temp);
}
- A EasyWorker or EasyCompute instance
- Create an instance of EasyWorker
// EasyWorker<Result Type, Input Type>
final worker = EasyWorker<int, int>(
Entrypoint(calculateFactorial),
workerName: "Factorial Calculator",
initialMessage: 0, // Optional: Initial payload for this worker will be 0
);
await worker.waitUntilReady();
- Now how to send and receive to and from this worker?
- get the first result only
final result = await worker.stream.first;
- Listen to all the results coming from isolate
worker.onMessage((message) {
print("Message From Worker: $message");
});
- Send Message to the worker
/// send 6 as payload to the worker to get the factorial of
/// 6.
worker.send(6);
- Create an instance of EasyCompute
// EasyCompute<Result Type, Input Type>
final worker = EasyCompute<int, int>(
ComputeEntrypoint(calculateFactorial),
workerName: "Factorial Calculator",
);
await worker.waitUntilReady();
- Get results
final result = await worker.compute(6);
print(result); //
worker.dispose()
- What about simple one time tasks?
/// just call compute and pass your entrypoint and payload
final result = await EasyWorker.compute<int, int>(calculateFactorial, 5);
print(result); // 120
FAQ:
- Why this package if there are already other who are doing the same?
- Basically all of them just have a learning curve and there technicalities, this package doesn't.
- I want to implement something. how to do it?
- You can create a feature request on github.
- MIT