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

1

-1

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://mcmap.net/q/210481/-how-to-update-state-of-a-modalbottomsheet-in-flutter

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!

Gertrudis answered 12/12, 2020 at 1:40 Comment(1)
Sorry. I guess I shouldn't have asked here.Gertrudis
A
0

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

Alkahest answered 12/12, 2020 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.