cancelable_retry
Utility for wrapping an asynchronous function in automatic retry logic with ability to cancel it.
Features
Allows to automatically retry request on some condition. Retry logic implemented with exponential back-off, useful when making requests over network. If you don’t need to continue retry – you can cancel retry. In such case last result will be returned.
Getting started
To use this plugin, add cancelable_retry
as a dependency in your pubspec.yaml file.
Usage
Create instance of CancelableRetry
and call run()
:
import 'package:cancelable_retry/cancelable_retry.dart';
final request = CancelableRetry(
// Provide request function
() => doSomeRequest(),
// Set conditions for retry
retryIf: (result) => result == "retry",
// Optional:
// - Define max retry attempts
maxAttempts: 8,
// - Define max delay between retries
maxDelay: const Duration(seconds: 30),
// - Tune delay between retries
delayFactor: const Duration(milliseconds: 200),
randomizationFactor: 0.25,
);
// Run request
final res = await request.run();
If you want to cancel retries, just call cancel()
:
await request.cancel();