retry method

Future<T> retry({
  1. int retries = 3,
  2. Duration delay = const Duration(milliseconds: 500),
})

Retries Future if it fails

Implementation

Future<T> retry({
  int retries = 3,
  Duration delay = const Duration(milliseconds: 500),
}) async {
  for (int attempt = 1; attempt <= retries; attempt++) {
    try {
      return await this;
    } catch (e) {
      if (attempt == retries) rethrow;
      await Future.delayed(delay);
    }
  }
  throw Exception("Retry failed after $retries attempts");
}