retry method

Future<T> retry(
  1. int times, {
  2. Duration delay = Duration.zero,
})

Retries this future up to times attempts if it throws an error.

An optional delay can be applied between retries. If all attempts fail, the last captured error is thrown.

Example:

await apiCall().retry(3, delay: const Duration(seconds: 1));

Implementation

Future<T> retry(int times, {Duration delay = Duration.zero}) async {
  late Object error;

  for (var i = 0; i < times; i++) {
    try {
      return await this;
    } catch (e) {
      error = e;

      if (delay != Duration.zero) {
        await Future.delayed(delay);
      }
    }
  }

  throw error;
}