title: What is the best way to track the state of an Isolate in Dart?
body: <p>I'm trying to track whether the isolate is currently running or not (and in the future whether it has errored out) using isolate.addOnExitListener(...).  However, the following snippet of code is not working how I would expect:</p>
  
  
  
  <pre class="lang-dart prettyprint-override"><code>items.forEach((name, item) async {
        Isolate isolate = await Isolate.spawnUri(...);
        item.status = "running";
        ReceivePort receivePort = new ReceivePort();
        isolate.addOnExitListener(receivePort.sendPort);
        receivePort.listen((message){
          if (message == null) {
            print("Item exited: ${item.name}");
            item.status = "stopped";
          }
        });
  });
  </code></pre>
  
  <p>The "items" map contains 3 values, each with a distinct name: item1, item2, item3</p>
  
  <p>When I run this code, the only output I get is:
  "Item exited: item3"</p>
  
  <p>I expected the following output (not necessarily in order since isolates are asynchronous):
  "Item exited: item1"
  "Item exited: item2"
  "Item exited: item3"</p>
  
  <p>Here is the code being run in the isolates:
  </p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'dart:io';
  main(List args) {
    print('Hello world: standard out!');
    stderr.writeln('Hello world: standard error!');
  }
  </code></pre>
  
  <p>It seems like the closure is being lost.  Am I doing something wrong here?  Is there a better way to track the state of an isolate?</p>
  
  <p>Thanks in advance!</p>