retryWhen method

Stream<T> retryWhen(
  1. bool shouldRetry(
    1. Object other
    ), {
  2. int maxRetries = 3,
  3. Duration delay = const Duration(seconds: 1),
})

Implementation

Stream<T> retryWhen(
  bool Function(Object error) shouldRetry, {
  int maxRetries = 3,
  Duration delay = const Duration(seconds: 1),
}) async* {
  var retries = 0;
  while (true) {
    try {
      await for (final item in this) {
        yield item;
      }
      break;
    } catch (e) {
      if (retries >= maxRetries || !shouldRetry(e)) rethrow;
      retries++;
      await Future.delayed(delay);
    }
  }
}