withRetry method

Future<T> withRetry({
  1. int maxRetries = 3,
  2. Duration delay = const Duration(seconds: 1),
})

Implementation

Future<T> withRetry({
  int maxRetries = 3,
  Duration delay = const Duration(seconds: 1),
}) async {
  int retries = 0;
  while (retries < maxRetries) {
    try {
      return this;
    } catch (e) {
      retries++;
      if (retries == maxRetries) {
        rethrow;
      }
      await Future.delayed(delay);
    }
  }
  throw Exception("Retry failed");
}