'Call async function from Isolate function
I am trying to call an async function from the Isolate function.
class IsolateExample {
final ReceivePort port = new ReceivePort();
IsolateExample(){
Isolate.spawn(isolateFunction, port.sendPort);
}
static isolateFunction(SendPort port){
print('inside isolateFunction');
asyncFunction();
}
static void asyncFunction() async {
print('inside asyncFunction');
}
}
Usage of above class:
final IsolateExample _isolate = new IsolateExample();
Above code looks simple but asyncFunction never gets called. I do not have any clue why this is failing.
Solution 1:[1]
Isolate will only run your compute once. If you'd like to call the function inside the isolate in intervals, you can utilize Timer.periodic() similar to how it's demonstrated on this post.
Once you're done running the function inside the isolate, you can terminate it with isolate.kill()
- given that isolate
contains Isolate.spawn()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Omatt |