runWithRetry function

Future<void> runWithRetry({
  1. required FutureOr<void> callback(),
  2. required int maxRetries,
  3. Duration retryDelay = const Duration(milliseconds: 250),
  4. FutureOr<bool> stopCondition()?,
  5. FutureOr<void> onRetry(
    1. int attempt
    )?,
})

Runs callback and retries if an exception is thrown.

  • maxRetries the maximum number of times callback will be ran before rethrowing an exception if it does not complete successfully.
  • retryDelay the time to wait between retry attempts.
  • stopCondition an optional callback that determines whether we should stop retrying, in addition to the condition that we must not retry more than maxRetries times. If the stopCondition is met, we will stop retrying without exception.
  • onRetry an optional callback that will be called if callback fails and we need to attempt a retry.

Implementation

Future<void> runWithRetry({
  required FutureOr<void> Function() callback,
  required int maxRetries,
  Duration retryDelay = const Duration(milliseconds: 250),
  FutureOr<bool> Function()? stopCondition,
  FutureOr<void> Function(int attempt)? onRetry,
}) async {
  for (var attempt = 1;
      attempt <= maxRetries && (await stopCondition?.call() != true);
      attempt++) {
    try {
      await callback();
      break;
    } catch (e) {
      if (attempt == maxRetries) {
        rethrow;
      }
      await onRetry?.call(attempt);
      await Future.delayed(retryDelay);
    }
  }
}