在Flutter中测量精确时间的最佳工具是什么

gc0ot86w  于 2022-12-24  发布在  Flutter
关注(0)|答案(3)|浏览(175)

在我们的代码中,我们希望在精确的毫秒数之后激活函数。
在当前代码中我们使用了Timer,但是我们想知道如何确保Timer是准确的,或者flutter中是否有其他工具可以给我们提供最准确的执行时间。

ds97pgxw

ds97pgxw1#

计时器是一个很好的小部件,你可以使用持续时间和设置毫秒和秒等。

xwbd5t1u

xwbd5t1u2#

你可以用实时时钟来测试你的计时器,看看它是否准确。如果你想精确,那么你可以在持续时间方法中使用多个参数:

Timer(Duration(seconds: seconds, milliseconds: milliseconds, microseconds: microseconds), handleTimeout);
mec1mxoz

mec1mxoz3#

使用Ticker,它基于屏幕刷新率。创建一个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()控制它
别忘了用完后处理掉。

相关问题