How to update value from long running isolate in dart?

104 Views Asked by At

I am trying to update long running(while true) isolate. the idea is call update everywhere on main or update that value on future.

void main() async {
  final ReceivePort p = ReceivePort();
  var events = StreamQueue<dynamic>(p);
  SendPort sendPort = await events.next;
  await Isolate.spawn(longTask, p.sendPort);
  sendPort.send(30);

  Future.delayed(Duration(seconds: 10), (() {
    sendPort.send(10);
  }));
  Future.delayed(Duration(seconds: 50), (() {
    sendPort.send(30);
  }));
}

Future<void> longTask(SendPort p) async {
  final commandPort = ReceivePort();
  p.send(commandPort.sendPort);

  //###############################################
  // the idea is this loop will call function each second  
  while (true) {
    // how to get value 30 20 10  ?
    // here
    sleep(const Duration(seconds: 1));
  }
}

I cannot using await for because loop must running without stop

 await for (var d in commandPort) {
    print(d);
 }
0

There are 0 best solutions below