What specific types of data can actually be sent as a message between two separate isolates in Dart, and why can an arbitrary complex object, such as one containing an active network connection, genuinely not simply be passed directly between them?
Short answer
Only genuinely specific kinds of data can actually be sent as a message between two separate isolates, including primitive types like numbers, strings, and booleans, along with lists, maps, and certain specific other designated types, since Dart needs to be able to properly and…
Advertisement
AModel answer
Only genuinely specific kinds of data can actually be sent as a message between two separate isolates, including primitive types like numbers, strings, and booleans, along with lists, maps, and certain specific other designated types, since Dart needs to be able to properly and correctly serialize that specific data being sent in order to safely transfer it across the genuinely completely separate memory boundary that exists between the two isolates, and an arbitrary complex object such as one holding an active network connection or a live open file handle genuinely cannot be sent directly, since these ultimately depend on specific external native resources that simply cannot be meaningfully or safely serialized and properly recreated within an entirely different separate isolate.
// This works: primitive types and simple collections
sendPort.send({'status': 'complete', 'count': 42});
// This does not directly work: an object holding a live network connectionReal-world example
A developer attempting to directly pass an active Dio HTTP client instance to a background isolate discovers this genuinely does not actually work, and instead properly redesigns their code to instead pass just the plain specific request parameters needed, then construct a completely fresh new Dio client directly within that separate isolate itself.
Likely follow-ups
- What is the specific technical term Dart actually uses to describe this exact same overall message passing mechanism between isolates?
- Are there any newer specific exceptions to this general rule for certain particular kinds of objects?
Related topics