BackOff<T> class

Object holding options for retrying a function.

With the default configuration functions will be retried up-to 7 times (8 attempts in total), sleeping 1st, 2nd, 3rd, ..., 7th attempt:

  1. 400 ms +/- 25%
  2. 800 ms +/- 25%
  3. 1600 ms +/- 25%
  4. 3200 ms +/- 25%
  5. 6400 ms +/- 25%
  6. 12800 ms +/- 25%
  7. 25600 ms +/- 25%

Example

final response = await backOff(
  // Make a GET request
  () => http.get('https://google.com').timeout(Duration(seconds: 5)),
  // Retry on SocketException or TimeoutException
  retryIf: (e) => e is SocketException || e is TimeoutException,
);
print(response.body);

Constructors

BackOff(FutureOr<T> func(), {Duration delayFactor = const Duration(milliseconds: 200), double randomizationFactor = 0.25, Duration maxDelay = const Duration(seconds: 30), int maxAttempts = 8, FutureOr<bool> retryIf(Object error, int attempt)?})
const

Properties

delayFactor Duration
Delay factor to double after every attempt.
final
func FutureOr<T> Function()
The Function to execute. If the function throws an error, it will be retried maxAttempts times with an increasing delay between each attempt up to maxDelay.
final
hashCode int
The hash code for this object.
no setterinherited
maxAttempts int
Maximum number of attempts before giving up, defaults to 8.
final
maxDelay Duration
Maximum delay between retries, defaults to 30 seconds.
final
randomizationFactor double
Percentage the delay should be randomized, given as fraction between 0 and 1.
final
retryIf → (FutureOr<bool> Function(Object error, int attempt)?)
Function to determine if a retry should be attempted.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

call() Future<T>
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited