Implement manual retry with OfflineRetryController
masterThe OfflineRetryController allows you to manage manual connectivity retries with exponential backoff.
Key features:
- Exponential backoff: Automatically increases delay (1s, 2s, 4s, 8s, 16s).
- Configurable: Set
maxRetriesandretryCooldown. - Reactive: Extends
ChangeNotifier, so you can add a listener to triggersetState()when retry status changes. - Auto-reset: Resets automatically upon reconnection.
To use it, instantiate the controller, add a listener, and pass it to the retryController property of an OfflineBuilder.
class _MyWidgetState extends State<MyWidget> {
late final OfflineRetryController _retryController;
@override
void initState() {
super.initState();
_retryController = OfflineRetryController(
maxRetries: 5,
retryCooldown: const Duration(seconds: 2),
);
// Listen to changes to update UI (e.g., retry count or status)
_retryController.addListener(() => setState(() {}));
}
@override
void dispose() {
_retryController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return OfflineBuilder(
retryController: _retryController,
connectivityBuilder: (context, connectivity, child) {
final connected = !connectivity.contains(ConnectivityResult.none);
return Column(
children: [
Text(connected ? 'ONLINE' : 'OFFLINE'),
if (!connected)
ElevatedButton(
// Use .canRetry to check if more attempts are allowed
onPressed: _retryController.canRetry ? _retryController.retry : null,
child: Text('Retry (${_retryController.retryCount}/5)'),
),
],
);
},
);
}
}