1

I have a timer that is called from a button OnPressed: startTimeOut(60).

startTimeout(mins) async {

  await subscription.resume();
  print("susbscription started");
  return new Timer(Duration(minutes: mins), handleTimeout);
}
void handleTimeout() async {
    await subscription.cancel().then((_) {

      print("susbscription canceled");
    });

But the user has the possibility to abort the subscription stream by calling startTimeOut(0). In this case "susbscription canceled" get's printed but the first timer call is still active, so the subscription goes on merrily till the 60 minutes are over. Then "susbscription canceled" is printed again.

How could I sort of overwrite/cancel the first call to Timer?

1 Answer 1

0

You will need to keep a reference to the Timer instance and call cancel on subsequent calls to startTimeout.

4
  • Thank u Nate. Thing is that's exactly what I'm having trouble in doing. Tried to put it in a variable, so I could do something like varTimer.cancel() in the timeOut, but the compiler does not accept something like: return new varTimer(Duration(minutes: mins), handleTimeout);
    – Jaime
    Feb 8, 2019 at 3:11
  • Declare the variable with Timer timeout;. Assign with timeout = Timer(...); return timeout;. The variable with need to be in a scope outside of startTimeout - without knowing more context it's hard to say if it needs to be global or if there is some class in which it can be an field... You might want to read through the language tour: dartlang.org/guides/language/language-tour
    – Nate Bosch
    Feb 8, 2019 at 3:21
  • pls check out my modified code with the timeout var u suggested.
    – Jaime
    Feb 10, 2019 at 4:44
  • Not sure why you have an await on the call to subscription.pause() - that should return void... Assigning a global var like that should work, there must be something missing in the context here.
    – Nate Bosch
    Feb 11, 2019 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.