using timer in flutter code example

Example 1: setinterval flutter

// runs every 1 second
Timer.periodic(new Duration(seconds: 1), (timer) {
   debugPrint(timer.tick.toString());
});

Example 2: flutter time count

Timer _timer;
  int seconds = 0;
  int minutes = 0;
  int hours = 0;
  void startTimer() {
    const oneSec = const Duration(seconds: 1);
    _timer = new Timer.periodic(
      oneSec,
      (Timer timer) => setState(
        () {
          if (seconds < 0) {
            timer.cancel();
          } else {
            seconds = seconds + 1;
            if (seconds > 59) {
              minutes += 1;
              seconds = 0;
              if (minutes > 59) {
                hours += 1;
                minutes = 0;
              }
            }
          }
        },
      ),
    );
  }

Tags:

Dart Example