我想每秒调用一个函数。我尝试了这个,但它没有奏效:
void main() {
Stopwatch stopwatch = new Stopwatch();
stopwatch.start();
int counter = 0;
while (true) {
if ((stopwatch.elapsedMilliseconds / 1000) >= counter) {
counter += 1;
//do something
}
}
}
答案 0 :(得分:3)
使用Timer课程。
main() {
int counter = 0;
new Timer.periodic(const Duration(seconds: 1), (timer) {
counter +=1;
// do something
});
}