Home » Android » android – How to set an Interval in Flutter?

android – How to set an Interval in Flutter?

Posted by: admin May 14, 2020 Leave a comment

Questions:

Recently I tried to make an interval in flutter but I didn’t see anything like setInterval(function(){}, 1000) in JavaScript. Does it exist in Flutter?

How to&Answers:

you can use Timer for that.

Timer timer = new Timer(new Duration(seconds: 5), () {
   debugPrint("Print after 5 seconds");
});

EDITED

as pointed by @MoeinPorkamel in comments. Above answer is more like setTimeout instead of setInterval! Those who need interval, you can use:

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

To user Timer you need to import 'dart:async';