In 本文 https://www.raywenderlich.com/10971345-flutter-interview-questions-and-answers,他们产生了这样的分离株:
import 'dart:isolate';
void main() async {
final receivePort = ReceivePort();
final isolate = await Isolate.spawn(
downloadAndCompressTheInternet,
receivePort.sendPort,
);
receivePort.listen((message) {
print(message);
receivePort.close();
isolate.kill();
});
}
void downloadAndCompressTheInternet(SendPort sendPort) {
sendPort.send(42);
}
但我只能传入接收端口。我如何传递其他参数?
我找到了答案,所以我将其发布在下面。
由于只能传入单个参数,因此可以将参数设置为列表或映射。其中一个元素是 SendPort,其他项是您想要提供给函数的参数:
Future<void> main() async {
final receivePort = ReceivePort();
final isolate = await Isolate.spawn(
downloadAndCompressTheInternet,
[receivePort.sendPort, 3],
);
receivePort.listen((message) {
print(message);
receivePort.close();
isolate.kill();
});
}
void downloadAndCompressTheInternet(List<Object> arguments) {
SendPort sendPort = arguments[0];
int number = arguments[1];
sendPort.send(42 + number);
}
您已经失去了这样的类型安全性,但您可以根据需要检查方法中的类型。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)