waitUntil method
Will wait until the give condition returns true polling every pollInterval. If condition has not returned true
within the given timeout this will cause the returned future to complete with a TimeoutException.
Implementation
Future<void> waitUntil(
Future<bool> Function() condition, {
Duration? timeout = const Duration(seconds: 10),
Duration? pollInterval = const Duration(milliseconds: 500),
}) async {
return Future.microtask(
() async {
final completer = Completer<void>();
var maxAttempts =
(timeout!.inMilliseconds / pollInterval!.inMilliseconds).round();
var attempts = 0;
while (attempts < maxAttempts) {
final result = await condition();
if (result) {
completer.complete();
break;
} else {
await Future.delayed(pollInterval);
}
}
},
).timeout(
timeout!,
);
}