waitForCondition function
Waits for a specific condition to be true with a timeout.
Implementation
Future<void> waitForCondition(
bool Function() condition, {
Duration timeout = const Duration(seconds: 5),
Duration interval = const Duration(milliseconds: 100),
}) async {
final stopwatch = Stopwatch()..start();
while (!condition() && stopwatch.elapsed < timeout) {
await Future<void>.delayed(interval);
}
if (!condition()) {
throw Exception('Condition was not met within ${timeout.inMilliseconds}ms');
}
}