dispose method

void dispose()

Remove all SDK listeners and reset state. Resets the Calls SDK initialization and login state so that the next init() call will re-initialize and re-login the Calls SDK. This is necessary for the logout → re-login flow: the Chat SDK logout clears the native Calls SDK state, so we must re-init on next login.

Implementation

void dispose() {
  if (!_initialized) return;
  CometChat.removeCallListener(_listenerId);
  CometChatCallEvents.removeCallEventsListener(_listenerId);

  // End any active call session before clearing state.
  // Without this, the native Calls SDK session stays alive after logout,
  // causing "call is in progress" to persist on re-login.
  _endActiveSessionOnLogout();

  activeCall = null;
  _loggedInUser = null;
  _configuration = null;
  _cachedAuthToken = null;
  _callsSdkReady = false;
  _callsSdkLoginReady = false;
  _callsSdkCompleter = null;
  _callsSdkLoginCompleter = null;
  _initialized = false;

  // Reset service locators so they re-initialize with fresh
  // datasources on next login. Without this, the locators hold
  // references to stale instances from the previous session.
  CallOperationsServiceLocator.instance.reset();
  CallLogsServiceLocator.instance.reset();

  // Reset call state tracking in case a call was active during logout.
  CallStateService.instance.setActiveCallValue(false);
  CallStateService.instance.setActiveIncomingValue(false);
  CallStateService.instance.setActiveOutgoingValue(false);

  // Dismiss any visible incoming call overlay.
  IncomingCallOverlay.dismiss();

  // Logout from the Calls SDK so its internal state is fully cleared.
  // On next init(), we'll re-init and re-login fresh.
  CometChatCalls.logout(
    onSuccess: (_) {
      developer.log('CallEventService: Calls SDK logout successful');
    },
    onError: (e) {
      developer.log('CallEventService: Calls SDK logout error: ${e.message}');
    },
  );

  developer.log('CallEventService disposed');
}