Persistent 30-Second Timer in Flutter: Keeping the Countdown Running Even After App Closure

98 Views Asked by At

I am developing a Flutter app that runs a 30-second countdown timer continuously, even when the app is closed. This timer should never stop under any circumstances. I am aware that I can achieve this using Flutter's timer API or other Flutter APIs. However, my challenge is to ensure that this timer displays the same countdown time for all users.

Currently, my app has 15,000 active users, and I want this timer to show the same countdown for everyone, as I cannot predict when each user will open the timer screen. Can someone please explain the best method to achieve this functionality in Flutter or suggest any other methods that might be available?

I have implemented a basic timer in my Laravel backend. When the app is open, I retrieve the current server time.

"serverTime": "2023-09-04T05:45:05.210864Z",
"seconds": "5",

After obtaining the current server time, I utilize Timer.periodic in Flutter to manage the countdown timer by decrementing the seconds. However, this approach does not guarantee uniform countdown synchronization across all devices.

Future<void> _startTimer() async {
    countdownTimer.value = myServerSeconds.value;
    _timer = Timer.periodic(
      const Duration(milliseconds: 1000),
      (timer) async {
        if(countdownTimer.value == 0) {
          countdownTimer.value = 59;
        } else {
          countdownTimer.value--;
        }
      },
    );
}

Can anyone help me achieve my desired function?

0

There are 0 best solutions below