在我们的代码中,我们希望在精确的毫秒数之后激活函数。在当前代码中我们使用了Timer,但是我们想知道如何确保Timer是准确的,或者flutter中是否有其他工具可以给我们提供最准确的执行时间。
ds97pgxw1#
计时器是一个很好的小部件,你可以使用持续时间和设置毫秒和秒等。
xwbd5t1u2#
你可以用实时时钟来测试你的计时器,看看它是否准确。如果你想精确,那么你可以在持续时间方法中使用多个参数:
Timer(Duration(seconds: seconds, milliseconds: milliseconds, microseconds: microseconds), handleTimeout);
mec1mxoz3#
使用Ticker,它基于屏幕刷新率。创建一个stateful小工具SingleTickerProviderStateMixin:
stateful
SingleTickerProviderStateMixin
late final Ticker _ticker; late Duration _duration; @override void initState() { super.initState(); _duration = Duration.zero; _ticker = this.createTicker((elapsed) { setState(() { _duration = elapsed; }); }); }
你可以用_ticker.start() & _ticker.stop()控制它别忘了用完后处理掉。
_ticker.start()
_ticker.stop()
3条答案
按热度按时间ds97pgxw1#
计时器是一个很好的小部件,你可以使用持续时间和设置毫秒和秒等。
xwbd5t1u2#
你可以用实时时钟来测试你的计时器,看看它是否准确。如果你想精确,那么你可以在持续时间方法中使用多个参数:
mec1mxoz3#
使用Ticker,它基于屏幕刷新率。创建一个
stateful
小工具SingleTickerProviderStateMixin
:你可以用
_ticker.start()
&_ticker.stop()
控制它别忘了用完后处理掉。