简体   繁体   English

有没有办法为 Flutter StatefulBuilder 设置一个定期计时器,以每 1 秒用 setState() 更新一次?

[英]Is there a way to set a periodic timer for a Flutter StatefulBuilder to update with setState() every 1 second?

I found this help on StatefulBuilder and it shows how to use setState to update things in a ModalBottomSheet.我在 StatefulBuilder 上找到了这个帮助,它展示了如何使用 setState 来更新 ModalBottomSheet 中的内容。 But I want to go one more step and make a timer to do this.但我想再走一步 go 并做一个计时器来做到这一点。

https://stackoverflow.com/a/56972160/559525 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");
  });
});

This prints out every second but the setState() isn't within the "builder" so nothing in the bottom tab updates.这会每秒打印一次,但 setState() 不在“构建器”中,因此底部选项卡中没有任何内容更新。 But if I put it in the builder it creates a LOT of timers.但是如果我把它放在构建器中,它会创建很多计时器。

Any help would be appreciated!任何帮助,将不胜感激!

The correct approach would be to create the Timer in the initState method, not build .正确的方法是在initState方法中创建Timer ,而不是build

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM