close method
Closes the instance. This method should be called when the instance is no longer needed. Once close is called, the instance can no longer be used.
Implementation
@override
Future<void> close() async {
// Bump the epoch and force-reset the confirmation flag at the very TOP,
// BEFORE any cleanup (NET-018/INV-4): bloc's `isClosed` only flips at
// `super.close()` (which runs last), so during teardown the epoch is the
// only live fence stopping an in-flight continuation (a startup-retry
// iteration, a confirmation probe, a resume re-probe) from dereferencing the
// just-nulled `_connectionChecker`.
_networkEpoch++;
_confirmingOffline = false;
// Clean up all stream subscriptions and services
// Use try-catch for each to ensure all cleanup happens even if one fails
try {
await _connectionCheckerSubscription?.cancel();
_connectionCheckerSubscription = null;
} catch (e) {
loge(e, 'Error canceling connection checker subscription');
}
// Dispose the checker (added in iccp 3.1.0; the pubspec floor guarantees
// it exists) so its timer + trigger subscription are torn down — otherwise
// an abandoned checker keeps polling (the BEH-3 leak).
try {
await _connectionChecker?.dispose();
_connectionChecker = null;
} catch (e) {
loge(e, 'Error disposing connection checker');
}
try {
await _versionUpdateSubscription?.cancel();
} catch (e) {
loge(e, 'Error canceling version update subscription');
}
try {
await _lifecycleSubscription?.cancel();
} catch (e) {
loge(e, 'Error canceling lifecycle subscription');
}
try {
await _notificationBadgeSubscription?.cancel();
} catch (e) {
loge(e, 'Error canceling notification badge subscription');
}
try {
AppVersionUpdateService().dispose();
} catch (e) {
loge(e, 'Error disposing AppVersionUpdateService');
}
try {
AppLifecycleService().dispose();
} catch (e) {
loge(e, 'Error disposing AppLifecycleService');
}
// Always call super.close() even if cleanup fails
return super.close();
}