Moved a big JSON decode off the main isolate with Isolate.run and got an error about an unsendable object instead of a speedup. The decode itself was fine. The closure was the problem.
class Loader {
final updates = StreamController<int>();
List<Object?> _decode(String text) => jsonDecode(text) as List<Object?>;
Future<List<Object?>> parse(String text) {
return Isolate.run(() => _decode(text));
}
}
_decode is an instance method, so the closure holds this, and this holds the StreamController. Everything the closure can reach is sent to the new isolate, and a controller can't be sent. Making _decode a static method fixed it. What I still don't know is how to spot this before running it, because the analyzer is perfectly happy with the code above.