0Pricing
Dart Academy · Lesson

Spawning Isolates and Message Passing

Communicate through SendPort and ReceivePort.

Spawning Isolates and Message Passing is a free Dart Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Going Manual

For long-lived, chatty workers you spawn an isolate yourself with Isolate.spawn. You stay in control of its whole lifetime.

The Entry Point

Isolate.spawn needs a top-level function as its entry point, plus one argument to send it at startup.

Isolate.spawn(worker, message);

Meet ReceivePort

A ReceivePort is your mailbox. You listen on it to receive messages that other isolates send your way. 📥

final port = ReceivePort();
port.listen((msg) => print(msg));

Meet SendPort

Every ReceivePort exposes a SendPort. Whoever holds that SendPort can drop messages into your mailbox.

final SendPort tx = port.sendPort;

Handing Over a Port

To start talking, give the new isolate a SendPort as its spawn argument. Now it knows where to reply.

Isolate.spawn(worker, port.sendPort);

The Worker Replies

Inside the worker, use the SendPort you received to send results back to the main isolate.

void worker(SendPort tx) {
  tx.send('done');
}

The Handshake

A common pattern is a two-way handshake: the worker sends back its own SendPort first, so both sides can message each other.

Only Sendable Data

Messages must be sendable: numbers, strings, lists, maps, and SendPorts. Things like open files or functions cannot cross the boundary.

Reading Replies

Back home, your ReceivePort's listen callback fires for each reply. Handle them as they arrive, in order.

Closing the Port

When you are finished, call port.close(). An open ReceivePort keeps your program alive, so closing it lets things exit cleanly.

Killing an Isolate

Hold the Isolate reference and call isolate.kill() to stop a worker you no longer need. Free its resources promptly.

Quick Check

Which port do you listen on to receive messages?

Recap

Spawn with Isolate.spawn, then pass a SendPort so isolates can message each other. Send only sendable data, and close ports when done. ✅

Frequently asked questions

Is the “Spawning Isolates and Message Passing” lesson free?

Yes — the full text of “Spawning Isolates and Message Passing” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “Spawning Isolates and Message Passing”?

Communicate through SendPort and ReceivePort. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Spawning Isolates and Message Passing” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Isolates, Not Threads
  2. Isolate.run for Quick Offloading
  3. Spawning Isolates and Message Passing
  4. Compute-Heavy Tasks Without Jank
← Back to Dart Academy