如何在颤振中停止计时器? [英] How to stop the timer in flutter?

查看:27
本文介绍了如何在颤振中停止计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 flutter 中创建了一个计时器,一切正常.现在我不知道如何在启动计时器后关闭它.
文档说你可以通过调用 void cancel() 取消它,但我不明白实现.

I have created a timer in flutter and everything works fine. Now I can't figure out how to dismiss the timer after I started it.
The docs say that you can cancel it by calling void cancel() but I don't understand the implementation.

我该怎么称呼它?
这是正确的方法吗?

How am I supposed to call it?
And is it the right approach?

  static const timeout = const Duration(seconds: 5);
  static const ms = const Duration(milliseconds: 1);

  startTimeout([int milliseconds]) {
    var duration = milliseconds == null ? timeout : ms * milliseconds;
    return new Timer(duration, handleTimeout);
  }

  void handleTimeout() {  // callback function
    Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(
        builder: (BuildContext context) => new ScorePage(quiz.score, quiz.length)),(Route route) => route == null);
    return;
  }

推荐答案

只要保留对时间的引用,当你不再需要时取消它

Just keep a reference to the time and cancel it when you don't need it anymore

var timer = startTimeout(100);
...
timer.cancel();

这篇关于如何在颤振中停止计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆