retryFuture<T> function

Future<T> retryFuture<T>(
  1. Future<T> func(), {
  2. int retries = 3,
})

Retries a Future-returning function a specified number of times.

This is a convenience wrapper around RxDart's Rx.retry() that provides a Future-based API. If the function succeeds within the retry limit, the result is returned. If it fails after all retries, the last error is thrown.

func - The Future-returning function to retry. retries - The number of retry attempts (default is 3).

Example:

final result = await retryFuture(() async {
  final response = await http.get(url);
  if (response.statusCode != 200) throw Exception('Failed');
  return response.body;
}, retries: 3);

Implementation

Future<T> retryFuture<T>(Future<T> Function() func, {int retries = 3}) {
  return Rx.retry(() => func().asStream(), retries).first;
}