Isolate.run or compute fails with Illegal argument in isolate message
Your error
Invalid argument(s): Illegal argument in isolate message: object is unsendable - Library:'dart:ui' Class:2 more lines it can showFix it fast
Most common cause, 1 of 2The argument or the result is itself a dart:ui object
Move only plain data across: compute the coordinates in the background, return them as doubles or a Float32List, and build the Path on the main isolate.
// import 'dart:math' as math; import 'dart:typed_data';
// Background: pure Dart math, returns plain numbers.
Float32List computePoints(int count) {
final points = Float32List(count * 2);
for (var i = 0; i < count; i++) {
points[i * 2] = i.toDouble();
points[i * 2 + 1] = math.sin(i / 10) * 100;
}
return points;
}
// Main isolate: turn the numbers into a Path.
final points = await compute(computePoints, 2000);
final path = Path()..moveTo(points[0], points[1]);
for (var i = 2; i < points.length; i += 2) {
path.lineTo(points[i], points[i + 1]);
}Short answer
Illegal argument in isolate message: object is unsendable - Library:'dart:ui' means the data sent to Isolate.run or compute held a native-backed object such as a Path, Picture or Image, directly or through a captured closure variable. Send doubles or typed lists instead, build the Path on the main isolate, and pass only the fields needed. Dart 3.x.
Which cause do you have?
Read the lines that start with <- under the error. The VM prints the retaining path from the unsendable object up to your message: if it ends at your argument or return value, the first cause applies; if it runs through a Context or a Closure, the second does.
flutter run 2>&1 | grep -A12 "Illegal argument in isolate message"Then find the line you see in this list and open its fix:
- 1The argument or the result is itself a dart:ui object
The retaining path is short: the Path (shown as a private class such as _NativePath) sits in the list, map or object you pass to compute or return from it - 2A closure passed to Isolate.run captured state that holds the Path
The <- lines pass through a Context num_variables entry or a captured variable of a closure, or name a field of your State, painter or controller class
What does the error look like?
Invalid argument(s): Illegal argument in isolate message: object is unsendable - Library:'dart:ui' Class:(see restrictions listed at `SendPort.send()` documentation for more information)Illegal argument in isolate message: (object extends NativeWrapper - Library:'dart:ui' Class: Path)
Cause 1 of 2
The argument or the result is itself a dart:ui object
You seeThe retaining path is short: the Path (shown as a private class such as _NativePath) sits in the list, map or object you pass to compute or return from it
Check
Illegal argument in isolate message: object is unsendable is raised when a message holds an object with native resources. SendPort.send allows almost any object between isolates that share code, but not subclasses of NativeFieldWrapperClass1, and in dart:ui the Path, Picture, Image and Canvas implementations all extend that class. compute and Isolate.run copy the callback, its input and its result this way, so a Path that goes in or comes back fails.
// Look for dart:ui types in what crosses the isolate boundary:
grep -rn -E "(compute|Isolate\.run)\(" lib/Fix
Move only plain data across: compute the coordinates in the background, return them as doubles or a Float32List, and build the Path on the main isolate.
// import 'dart:math' as math; import 'dart:typed_data';
// Background: pure Dart math, returns plain numbers.
Float32List computePoints(int count) {
final points = Float32List(count * 2);
for (var i = 0; i < count; i++) {
points[i * 2] = i.toDouble();
points[i * 2 + 1] = math.sin(i / 10) * 100;
}
return points;
}
// Main isolate: turn the numbers into a Path.
final points = await compute(computePoints, 2000);
final path = Path()..moveTo(points[0], points[1]);
for (var i = 2; i < points.length; i += 2) {
path.lineTo(points[i], points[i + 1]);
}Verify
compute completes, the painter draws the path, and the error no longer appears.
flutter runConfirmed by api.dart.dev
Cause 2 of 2
A closure passed to Isolate.run captured state that holds the Path
You seeThe <- lines pass through a Context num_variables entry or a captured variable of a closure, or name a field of your State, painter or controller class
Check
A closure handed to Isolate.run is sent together with the variables it closes over, and the Dart API docs warn that this can drag unexpected state along and fail at runtime when that state is unsendable. A lambda written inside a State or CustomPainter method that touches any field captures this, so a Path, Image or controller stored on the object goes with it even if the computation never uses it.
grep -rn -B2 -A6 "Isolate.run(() " lib/Fix
Move the Isolate.run call into a top-level or static function that receives only the values it needs, which is what the Isolate.run docs recommend, or hand a top-level function straight to compute so no closure is created at all.
// Top-level: the closure here can only see `values`.
List<double> smooth(List<double> values) {
// heavy work on plain numbers
return values;
}
Future<List<double>> smoothInBackground(List<double> values) =>
Isolate.run(() => smooth(values));
// Inside the State: pass a plain copy, nothing else.
final smoothed = await smoothInBackground(List<double>.of(_samples));
// or, without any closure:
final smoothed2 = await compute(smooth, List<double>.of(_samples));Verify
The isolate runs without the error, and the retaining path no longer appears in the log.
flutter runConfirmed by api.dart.dev
How do I stop it coming back?
- Keep background functions top-level or static and give them plain Dart values only; review any Isolate.run(() => ...) written inside a widget, painter or controller method.
- Treat every dart:ui type (Path, Picture, Image, Canvas, Shader) as main-isolate only, and convert it to numbers or bytes before work leaves that isolate.
- Add a test that calls the background function with Isolate.run on sample data, so an unsendable value is caught before release.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, Qist Bazaar, SessionBird.