waitFor function

Future<void> waitFor(
  1. Future<bool> condition(), {
  2. Duration timeout = const Duration(seconds: 10),
  3. String timeoutMessage = 'condition not satisfied',
  4. Duration delay = _shortDelay,
})

Implementation

Future<void> waitFor(
  Future<bool> Function() condition, {
  Duration timeout = const Duration(seconds: 10),
  String timeoutMessage = 'condition not satisfied',
  Duration delay = _shortDelay,
}) async {
  final end = DateTime.now().add(timeout);
  while (!end.isBefore(DateTime.now())) {
    if (await condition()) {
      return;
    }
    await Future.delayed(delay);
  }
  throw timeoutMessage;
}