Sub topic 5
What You Can Send, and What Gets Copied
This chapter assumes basic Dart syntax, async/await, Future, and streams. It also assumes that you understand that isolates have separate heaps and communicate with messages.
An isolate cannot directly read or change another isolate's variables. When you send a message, Dart transfers the message's object graph to the receiving isolate. For ordinary mutable objects, this means the receiver gets a copy. The two isolates can then change their own copies independently.
Common sendable values
Messages can contain simple values such as null, booleans, numbers, strings, lists, maps, records, and SendPort objects. Collections can contain other sendable values, so every value nested inside a list or map must also be suitable for transfer.
Dart may share some immutable objects internally instead of copying them. Treat this as an implementation detail. Your code should depend only on the rule that isolates do not share mutable state.
| Value | Usual message behavior | Example |
|---|---|---|
| Primitive value | Copied or safely shared | `42`, `'ready'`, `true` |
| Mutable collection | Copied | `<int>[1, 2, 3]` |
| `SendPort` | Port reference is transferred | `workerPort` |
| `TransferableTypedData` | Buffer ownership is transferred | Large byte data |
| Open resource or UI object | Usually unsupported | File handle, `BuildContext` |
A custom Dart object may be transferable when its fields form a transferable object graph. However, objects that wrap native resources, open ports, locks, or framework state are not suitable. A State object, BuildContext, database connection, or controller should stay in the isolate that created it.
Copying mutable data
Suppose the main isolate sends a list to a worker. The worker can sort or replace items in its list without changing the original list in the main isolate. This protects isolates from data races, but copying has a cost. Large nested collections can take time and memory to transfer.
Isolate.run handles the message transfer around a one-off computation:
import 'dart:isolate';
Future<List<int>> sortNumbers(List<int> input) {
return Isolate.run(() {
final copy = List<int>.from(input);
copy.sort();
return copy;
});
}
Future<void> main() async {
final original = [4, 1, 3, 2];
final sorted = await sortNumbers(original);
print(original); // [4, 1, 3, 2]
print(sorted); // [1, 2, 3, 4]
}The explicit List<int>.from makes the worker's ownership clear. The isolate boundary already prevents shared mutable state, but making a local copy can also prevent accidental reuse of an object captured from the caller.
Avoid accidental closure captures
A closure is the function value written inline, such as () => sumList(data). With Isolate.run, Dart must transfer the closure and the values it captures.
Capturing this can pull an entire object graph into the transfer. It can also fail if any referenced object is not transferable. Copy fields into local variables and pass only the data the worker needs.
import 'dart:isolate';
int sumList(List<int> values) =>
values.fold(0, (total, value) => total + value);
class Report {
Report(this.values, this.title);
final List<int> values;
final String title;
Future<int> total() {
final data = List<int>.from(values);
return Isolate.run(() => sumList(data));
}
}Here, the closure captures data, not the whole Report instance. The title field is not needed for the calculation and is not transferred.
Moving large byte buffers
For large binary data, ordinary transfer can copy the bytes. TransferableTypedData transfers ownership of the buffer instead. After the worker materializes the data, the worker can read it as a typed byte list.
import 'dart:isolate';
import 'dart:typed_data';
Future<int> countNonZero(Uint8List bytes) {
final transferable = TransferableTypedData.fromList([bytes]);
return Isolate.run(() {
final received = transferable.materialize().asUint8List();
var count = 0;
for (final byte in received) {
if (byte != 0) count++;
}
return count;
});
}
Future<void> main() async {
final bytes = Uint8List.fromList([0, 2, 0, 7]);
print(await countNonZero(bytes)); // 2
}A TransferableTypedData value is intended for one transfer. Do not try to materialize it again after ownership has moved.
Exercise
Why should a worker receive a plain list of IDs instead of a repository object?
Show answer
A plain list has a small, clear transferable object graph. A repository may capture connections, native resources, or other unsendable state.