FLUTTER Task with timer running in background

Viewed 35

I have UI like this, and we can see timer running in my apps.

UI UX

And here's code of the timer :

TimerCountdown(
  onEnd: () {
    getTrxResponse();
    setState((){});
    if (trxStatus.contains('Pending')) {
      failedUpdate();
      setState((){});
    }
  },
  enableDescriptions: false,
  format: CountDownTimerFormat.hoursMinutesSeconds,
  endTime: now.add(
      const Duration(hours: 00, minutes: 30, seconds: 00)),
  timeTextStyle: const TextStyle(
      color: Colors.black45,
      fontWeight: FontWeight.bold,
      fontSize: 12),
  colonsTextStyle: const TextStyle(
      color: Colors.black,
      fontWeight: FontWeight.bold,
      fontSize: 8),
  spacerWidth: 2,
),

The target is when the timer on End will updating status transaction, then if status still 'Pending' app will sent failed transaction to backend.

The problem is when the apps get force closed by user, it will make the timer stop running and will not sent anything to backend.

Is there something I can do to make the timer running in background?

Or I used a wrong method?

1 Answers

My take on this:

If its a sensitive process that has to be done and confirmed by the User, and considering the multiple problems that can happen to the human user to complete the transaction on time, I would save the state in the backend like so:

1- On transaction start => save a temporal variable in the database linked to the user id and transactionID like ==> expireOn = DateTimeToExpire & transactionDone = false

2- In the frontend (your app) Set a recurring Timer to check the state in the server. IF currentDateTime > expireOn AND transactionDone = false ==> report a problem ELSE success.

3- onApp start / load do a check if you have pending transactions in the server and check if point #2 above is successfull or if you need to inform of pending or canceled transactions

with these steps you handle the problem of user closing/deleting the app, and notify or remind if they have pending transactions

Related