Tips to use Timer in Dart and Flutter

1
151783
Tips to use Timer in Dart and Flutter

Timer is essential part of any application, and is useful in many use-cases. Let me show you some tips to use Timer in Dart and Flutter.

Use Timer in Dart and Flutter

First of all, let take a look at the Timer API documentation.

Basically, Timer is a class that represents a count-down timer that is configured to trigger an action once end of time is reached, and it can fire once or repeatedly.

Make sure to import dart:async package to start of program to use Timer.

1. Trigger actions after countdown

See this example:

Timer(Duration(seconds: 3), () {
  print("Yeah, this line is printed after 3 seconds");
});

If you try to run it on Dart VM or Flutter app main() function, it will print the line after 3 seconds.

Should it run asynchronously? Let’s make a test for it.

Timer(Duration(seconds: 3), () {
  print("Yeah, this line is printed after 3 second");
});

print('This line is printed first');

What’s your expectation?

Well, in reality, the text This line is printed first is indeed printed first before the text print in the callback function. So the callback function of Timer is being executed asynchronously, it doesn’t block the main thread.

2. Repeat actions

In previous section, the timer will trigger the action only once then stops. What if you want do something repeatedly? Here it is.

Timer.periodic(Duration(seconds: 5), (timer) {
  print(DateTime.now());
});

Timer.periodic() is the function designed for this. However, the callback needs to pass an instance of Timer. For what purpose?

Well, you need to stop the timer at some points, cannot let it go free forever.

If you execute the above code, you might get output similar to this

2019-01-22 10:26:36.479593
2019-01-22 10:26:41.473463
2019-01-22 10:26:46.475937
2019-01-22 10:26:51.476327
2019-01-22 10:26:56.473401
2019-01-22 10:27:01.477890
2019-01-22 10:27:06.475911
2019-01-22 10:27:11.473604
2019-01-22 10:27:16.477881

So, the callback is triggered every 5 seconds as configured.

To stop timer when it reaches some certain expectations, call timer.cancel().

3. Trigger timer immediately

What if you want to to trigger action immediately in asynchronous mode?

You might think about configuring the duration to 0. Let’s try it.

Timer(Duration(seconds: 0), () {
  print("Yeah, this line is printed after 3 second");
});

print('This line is printed first');

However, don’t expect that the text in callback will be printed first. It, most of the time, will be output like following result:

This line is printed first
Yeah, this line is printed after 3 second

It is because all async functions are put into a pool to schedule, so it takes a little time for the callback to execute. Therefore, the second line in the code above is always printed first.

There is a shorthand for this, you can use Timer.run() to trigger callback as soon as possible.

Timer.run(() {
  print("Yeah, this must be printed immediately.");
});

Summary

Timer is a powerful utility for us to trigger actions in the background asynchronously. You can use it in both Dart and Flutter applications. Take advantages of Timer and use it properly!