Home > database >  what is the best tool to measure accurate time in flutter
what is the best tool to measure accurate time in flutter

Time:12-22

in our code we want to activate fucntion after a number of precise milliseconds.

In the current code we use Timer , but we want to know how to be sure the Timer is accurate or if there is another tool in flutter that can give us the most accurate execution time.

CodePudding user response:

Timer is good widget that tou can use Duration and set milliseconds and seconds and so on..

CodePudding user response:

You can test check your Timer with realtime clock to see if it is accurate or not. And if you want pinpoint accuracy then you can use multiple parameters in your Duration method:

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

CodePudding user response:

use Ticker, its based on screen refresh rate. create a stateful widget with SingleTickerProviderStateMixin :

late final Ticker _ticker;
late Duration _duration;

@override
  void initState() {
    super.initState();
    _duration = Duration.zero;
    _ticker = this.createTicker((elapsed) {
      setState(() {
        _duration = elapsed;
      });
    });
  }

you can control it with _ticker.start() && _ticker.stop()

don't forget to dispose it when your done.

  • Related