有没有一种方法可以设置Flutter StatefulBuilder的定期计时器,使其每隔1秒钟用setState()更新一次? [英] Is there a way to set a periodic timer for a Flutter StatefulBuilder to update with setState() every 1 second?

查看:75
本文介绍了有没有一种方法可以设置Flutter StatefulBuilder的定期计时器,使其每隔1秒钟用setState()更新一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在StatefulBuilder上找到了此帮助,它显示了如何使用setState更新ModalBottomSheet中的内容.但我想再走一步,安排一个计时器来做到这一点.

I found this help on StatefulBuilder and it shows how to use setState to update things in a ModalBottomSheet. But I want to go one more step and make a timer to do this.

https://stackoverflow.com/a/56972160/559525

我在有状态的生成器中创建这样的计时器:

I create the timer like this inside the stateful builder:

var bottomSheetTimer = Timer.periodic(const Duration(milliseconds: 1000), (timer) {
  setState(() {
    currSeekTime += 1;
    print("here");
  });
});

每秒打印一次,但setState()不在"builder"窗口中.因此底部标签中的所有内容均不会更新.但是,如果我将其放入构建器中,则会创建很多计时器.

This prints out every second but the setState() isn't within the "builder" so nothing in the bottom tab updates. But if I put it in the builder it creates a LOT of timers.

任何帮助将不胜感激!

推荐答案

正确的方法是在 initState 方法中创建 Timer ,而不是 build.

The correct approach would be to create the Timer in the initState method, not build.

这篇关于有没有一种方法可以设置Flutter StatefulBuilder的定期计时器,使其每隔1秒钟用setState()更新一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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