If you're writing Dart code and you need to delay code execution for any reason, here are some ways to create delayed execution
All the methods below use Dart's Duration
class to define the delay. It supports the following optional parameters:
days
hours
minutes
seconds
milliseconds
microseconds
Duration(seconds: 5)
means 5 second delay. If you want to add a delaay of 1 minute and 10 seconds, use Duration(minutes: 1, seconds: 10)
.
Using sleep
The most basic way to sleep code execution is using sleep
. However, be careful when using sleep
as it blocks the main thread. At that time, it cannot process asynchronous operations in an isolate.
print(DateTime.now());
sleep(new Duration(seconds: 10));
print(DateTime.now()); // This will be printed 10 seconds later.
Using Future.delayed
If you need to delay execution without blocking, you can use Future.delayed by passing a Duration object.
print(DateTime.now());
await Future.delayed(const Duration(seconds: 10));
print(DateTime.now()); // This will be printed 10 seconds later.
Using Timer
With Timer
, you can define a callback function that will be invoked after the duration.
print(DateTime.now());
new Timer(const Duration(seconds: 10), () => print(DateTime.now()));
Using Stream.periodic
Stream.periodic
is used to create a stream that repeatedly emits events at periodic intervals. The code below create a Stream.periodic
that will be executed for 100 times, with a delay of 10 seconds between two executions.
var count = 0;
final Stream myStream = new Stream.periodic(Duration(seconds: 10), (_) => count++);
myStream.map((val) {
count = val;
print('$count: ${DateTime.now()}');
return val;
}).take(100).forEach((e) {
print(e);
});
If you run any example above and looking at the printed time, you'll find out 10 seconds delay between printed dates.