Controlling while loops in Dart
<p>I am stuck trying to do something simple. I want to be able to count upwards until I click on the screen at which point i want the counting to stop. In reality the code itself will be carrying out complex AI calculations for a game, but first I want to understand how to control the while loop. In android this would be trivial.</p>

<p>Here is what my code looks like</p>

<pre><code>bool OK = true;

main() async{
 html.querySelector('#content').onMouseUp.listen((e){
   OK = false;
 });

 await for(int i in naturals){
   print(i);
   await sleep();
 }
}

Stream get naturals async* {
 int k = 0; while (OK) { yield await k++; }
}

Future sleep() {
 return new Future.delayed(const Duration(milliseconds: 1), () =&gt; "1");
}
</code></pre>

<p>I put the sleep() method in as a way to ensure that control is passed to the event loop. </p>

<p>Is it possible to control a while loop without the sleep() method?</p>