What are isolates?
- Dart course
- Advanced
- Lesson 1/83
- 5 min
Learn the Dart concurrency model with isolates.
What are isolates?
An isolate is Dart's unit of isolated execution. Each isolate has its own memory, its own event loop and its own stack.
Unlike threads, two isolates do not share memory; this removes the need for locks or mutexes. They communicate only by message passing — through SendPort and ReceivePort.
This design eliminates race conditions up front. Heavy work is moved to the background without blocking the main isolate.
How do you start an isolate?
A new isolate is started with Isolate.spawn(entryPoint, args). entryPoint must be a top-level function (or a static method); it cannot capture closure variables.
Signature: void entryPoint(List<String> args). The args list is passed as the second argument to spawn; typically the first element is a SendPort.
Example: Isolate.spawn(worker, ['gorev-1']);. This schedules the new isolate; the main isolate continues.
Practice Define the entryPoint function
Define a top-level void entryPoint(List<String> args) function. Its body should do print('Isolate ${args[0]} çalışıyor'). Inside main call this function with the argument ['gorev-1'].
Starter code
void main() {
// define the entryPoint function (outside main)
// add the call inside main
}Sample solution
void entryPoint(List<String> args) {
print('Isolate ${args[0]} çalışıyor');
}
void main() {
entryPoint(['gorev-1']);
}Expected output
Isolate görev-1 çalışıyor
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which statement about isolates is true?
- They have their own memory, not shared Correct answer
- They can only use a single CPU core
- They share memory and need locks
- It means the same thing as a thread
Explanation: each isolate owns its memory and does not share it.
2 Complete the spawn call
Isolate.spawn(
____,
['gorev-1'],
);
Isolate.spawn(
entryPoint,
['gorev-1'],
);
Explanation: spawn takes the entryPoint function as its first parameter.
3 Match the isolate concepts
| Term | Meaning |
|---|---|
Isolate |
Isolated worker |
SendPort |
Message sending channel |
Memory |
Unshared area |
Event loop |
Event queue |