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

611 Views Asked by At

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");
  });
});

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.

Any help would be appreciated!

1

There are 1 best solutions below

0
On

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