In Isolates, I can refer to a local variable from an outer scope or a field variable of a class without passing it as a separate message.
Is this implicitly copying the values into the new isolation's memory area?
I'm curious about the details.
示例
class Person {
Person(this._baseNum);
/// access [_baseNum] in isolate
final int _baseNum;
int age = 0;
/// access [extraAge] in isolate
Future<void> addAge(int extraAge) async {
final mainReceivePort = ReceivePort();
await Isolate.spawn((SendPort sendPort) async {
sendPort.send(await _calcAge(_baseNum, extraAge));
}, mainReceivePort.sendPort);
age = await mainReceivePort.first;
mainReceivePort.close();
}
static Future<int> _calcAge(int someNum, int age) async {
// ... heavy work ...
return age + someNum;
}
}
// ...
void main() {
test('test', () async {
final p = Person(10);
await p.addAge(3);
expect(p.age, 13);
});
}