Member-only story
FLUTTER INTERVIEW QUESTIONS
What is an Isolate and why would you need to use it sometimes?
First of all, you probably DO NOT NEED TO USE IT :)
But, let’s see…
Coming from Java world, it’s very weird to hear that Dart is a single-threaded language(well, this in fact is not true, but it’s using just one thread most of the time). I’m used to performing networking calls off Main thread, so I vas very surprised when I saw that Dart does those ones in its UI(main thread). For simple apps, Dart does all the heavy lifting for you, and regularly you don’t need to think about moving your code away from the Main thread. All tasks are divided into micro-tasks and executed synchronously on the same event loop, and as they are being executed, chunk by chunk, you have a feeling that they’re being executed in parallel.
void main() {
printIt('apples');
printIt('bananas');
}Future<void> printIt(String value) async {
for (int i = 0; i < 10; i++) {
await Future.delayed(Duration(milliseconds: 100)); //sleep 100ms
print('$value $i');
}
}
This is the output:
apples 0
bananas 0
apples 1
bananas 1
…
apples 9
bananas 9
Those two printIt functions are executed asynchronously, and you can see them printing their…