I have a function which periodically checks a condition and I want the function to ONLY proceed once the condition is met. This seems like such a basic task, but for some reason I cannot find a good way to solve this.
When I call the function below, I always get ‘job not done yet’ first and then after three seconds ‘job done’. How can I make it proceed to the return statement only after the counter == 3 ?
Sorry if this is such a noob thing to ask. I used Google, I promise, but to no avail.
Future<String> delayTest() async {
var counter = 0;
await Timer.periodic(const Duration(seconds: 1), (timer) {
counter++;
if (counter == 3) {
print('job done');
timer.cancel();
}
});
return ('job not done yet.');
}
2
Answers
You should wait until timer gets canceled. Something like this:
If you want to wait 3 seconds
You can use