如何在 Flutter 中设置间隔?
How to set an Interval in Flutter?
最近我试图在flutter中做一个间隔,但我没有在JavaScript中看到像setInterval(function(){}, 1000)
这样的东西。它存在于 Flutter 中吗?
您可以为此使用计时器。
Timer timer = new Timer(new Duration(seconds: 5), () {
debugPrint("Print after 5 seconds");
});
已编辑
正如 @MoeinPorkamel 在评论中指出的那样。以上答案更像是 setTimeout
而不是 setInterval
!需要间隔的可以用:
// runs every 1 second
Timer.periodic(new Duration(seconds: 1), (timer) {
debugPrint(timer.tick.toString());
});
要使用 Timer
,您需要 import 'dart:async';
最近我试图在flutter中做一个间隔,但我没有在JavaScript中看到像setInterval(function(){}, 1000)
这样的东西。它存在于 Flutter 中吗?
您可以为此使用计时器。
Timer timer = new Timer(new Duration(seconds: 5), () {
debugPrint("Print after 5 seconds");
});
已编辑
正如 @MoeinPorkamel 在评论中指出的那样。以上答案更像是 setTimeout
而不是 setInterval
!需要间隔的可以用:
// runs every 1 second
Timer.periodic(new Duration(seconds: 1), (timer) {
debugPrint(timer.tick.toString());
});
要使用 Timer
,您需要 import 'dart:async';