retryWithPolicy<T> function
Retries fn up to maxAttempts with delay between attempts.
retryIf decides whether a given error is even worth retrying: when it
returns false the error is rethrown immediately, without consuming the
remaining attempts or waiting delay. This is how you retry only transient
failures (a timeout, a 503) and fail fast on permanent ones (a 400, an
ArgumentError). When retryIf is null (default) every error is retried.
onRetry is invoked after each failed attempt that will be retried, with
the error and the 1-based attempt number — useful for logging or metrics. It
does NOT fire on the final attempt (which rethrows) or on a retryIf-vetoed
error.
Audited: 2026-06-12 11:26 EDT
Implementation
Future<T> retryWithPolicy<T>(
Future<T> Function() fn, {
int maxAttempts = 3,
Duration delay = retryPolicyDefaultDelay,
bool Function(Object error)? retryIf,
void Function(Object error, int attempt)? onRetry,
}) async {
int attempt = 0;
while (true) {
try {
return await fn();
} on Object catch (e, st) {
log('retryWithPolicy attempt $attempt', error: e);
attempt++;
// Non-retryable error: surface it now rather than burning attempts and
// delaying on something that will never succeed.
if (retryIf != null && !retryIf(e)) Error.throwWithStackTrace(e, st);
if (attempt >= maxAttempts) Error.throwWithStackTrace(e, st);
onRetry?.call(e, attempt);
await Future<void>.delayed(delay);
}
}
}