How to use setInterval/setTimeout in Dart SDK 0.4+


I realised that in current Dart SDK version methods like setTimeout, setInterval, clearTimeout, clearInterval aren’t part of Window class any more and they all moved to WorkerContext.
Is there any documentation on how to use them now? Do I need to create a new instance of WorkerContext every time I want to use them?


In addition to Timer mentioned by Chris, there is a Future-based API:

var future = new Future.delayed(const Duration(milliseconds: 10), doStuffCallback);

There is not yet direct support for cancelling a Future callback, but this works pretty well:

var future = new Future.delayed(const Duration(milliseconds: 10));
var subscription = future.asStream().listen(doStuffCallback);
// ...

Hopefully, there will soon be a Stream version of Timer.repeating as well.

Answered By – Sean Eagan

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *