[英]How to make one button visible when another is clicked in Flutter/Dart?
[英]How to make multiple subscription on one ReceivePort dart
我的任務是創建 N 個隔離區,以及一個將傳遞給這些隔離區的主 ReceivePort,通過使用這個 ReceivePort,我傳遞了函數所需的所有信息,以便為我提供理想的輸出。 它是第一次工作,但是,向這些隔離區發送的任何連續消息並接收它們都會引發此類錯誤
這是我的代碼
//main receive port
var controlPort = ReceivePort();
...
class _MainScreenState extends State<MainScreen> {
final ImagePicker _picker = ImagePicker();
Future<ui.Image>? image;
@override
void initState() {
for (int i = 0; i < Platform.numberOfProcessors; i++) {
Future<Isolate> isolate =
Isolate.spawn(Core.readAndMapFast, controlPort.sendPort);
}
}
@override
Widget build(BuildContext context) {
...
static void readAndMapFast(SendPort sendPort) async {
//sending SendPort of a created isolate to the main thread
ReceivePort receivePort = ReceivePort();
sendPort.send(receivePort.sendPort);
receivePort.first.then((message) {
//listening for a message coming from main thread and populating it with all needed
//data
message as Arguments;
Arguments arguments = Arguments(message.imgList, message.lutList,
message.imgHeight, message.imgWidth, message.widthLut, message.order);
//ecexuting all needed operations and after that sending message to the main thread
...
sendPort.send(output);
//in the main thread we are listening for messages from RecievePort we created at
//the very beginning
await for (dynamic message in controlPort.asBroadcastStream()) {
if (message is SendPort) {
//waiting for a message from isolates and if its type is SendPort i.e isolate sending
//its first message we are sending needed arguments to a function
message.send(args[i]);
} else {
message as FinalList;
counter++;
postEditList.add(message);
if (counter == numberOfWorkers) {
Stopwatch timerr = Stopwatch();
timerr.start();
for (int t = 0; t < postEditList.length; t++) {
FinalList list =
postEditList.where((element) => element.order == t).first;
bytesBuilder.add(list.imgSubList);
}
break;
}
}
}
我建議我必須使用 ReceivePort().asBroadcastStream(),但是我不能發送消息並用它做其他事情。 因為當它變成流之后,我們就沒有SendPort了。
我想出了解決辦法。 這很容易。 我們有一個主要的 ReceivePort,我們將使用它來跨隔離區和主線程進行通信,並且我們還創建了一個 BroadcastStream,用於監聽來自我們的 ReceivePort 的任何更新。
var controlPort = ReceivePort();
var broadcastStream = controlPort.asBroadcastStream();
然后你所要做的就是監聽 broadcastStream 而不是 controlPort 的更新
broadcastStream.listen((event) {});
我希望它對那些被困在同一問題上的人有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.