flutter - 如何取消特定计时器?

标签 flutter dart timer cancellation periodic-task

如果我为3个不同的功能运行3个不同的计时器,我该如何取消特定的计时器。


const oneSec = const Duration(seconds: 60);
        new Timer.periodic(
            oneSec,
            (Timer t) async =>
                await appLoggerRepository.putAppLogOnServer(jobName));

const oneSec = const Duration(seconds: 60);
        new Timer.periodic(
            oneSec,
            (Timer t) async =>
                await appLoggerRepository.uploadOrder(jobName));

const oneSec = const Duration(seconds: 60);
        new Timer.periodic(
            oneSec,
            (Timer t) async =>
                await appLoggerRepository.checkdevice(jobName));

~~

最佳答案

您必须创建一个计时器变量

Timer logTimer = Timer.periodic( //no need for the new keyword
            oneSec,
            (Timer t) async =>
                await appLoggerRepository.putAppLogOnServer(jobName));

//to cancel call cancel()
logTimer.cancel();

关于flutter - 如何取消特定计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62450262/

相关文章:

Flutter - 如何在 vscode 中过滤调试控制台

sockets - 我在哪里可以获得 dart.io RawSocket 示例?

android - 一旦应用程序进入后台,在一定持续时间后 flutter 运行代码?

c# - 定时器在 C# 中没有被重置

android - Flutter Firestore QuerySnapshot 没有 getter 'document 的实例

flutter - 如何监听 ("close"事件)到 flutter web 中的文件下载窗口?

android - GetCurrentLocation 在 Android 的调试中工作但在发布 apk 中不起作用

templates - 角 Dart : material input initial value

javascript - 使用 service worker 的带有通知的计时器

ios - 在后台计数(NSTimer超过3分钟)