I have code similar to:
timer = new Timer(new Duration(milliseconds: 1000), () => (throw new TimeoutException('Callback not invoked!')));
while (timer.isActive){
await new Future.delayed(const Duration(milliseconds: 1), () => "1");
}
print('this should not be reached if the exception is raised');
elsewhere I have an async callback which calls:
timer.cancel();
In the case where the callback is invoked it works fine because the callback cancels the timer.
However, I'm not really sure how to actually catch the TimeoutException
in this case if it is not canceled, because it seems the exception is raised in a different scope than my main function. This means program execution continues even though
Is there a way to do some sort of try/catch or somehow handle the above timeout exception? Or a better way to do what I am trying to do?
Using dart 1.19.1.