retry method

Future<T> retry({
  1. int times = 3,
  2. Duration delay = const Duration(seconds: 1),
  3. bool retryIf(
    1. Object error
    )?,
})

Implementation

Future<T> retry({
  int times = 3,
  Duration delay = const Duration(seconds: 1),
  bool Function(Object error)? retryIf,
}) async {
  for (var i = 0; i < times; i++) {
    try {
      return await this;
    } catch (e) {
      final shouldRetry = retryIf?.call(e) ?? true;
      if (i == times - 1 || !shouldRetry) rethrow;
      await Future.delayed(delay);
    }
  }
  return await this;
}