logout static method

Future<void> logout({
  1. required dynamic onSuccess(
    1. String message
    )?,
  2. required dynamic onError(
    1. CometChatException excep
    )?,
})

Method to log out the user from CometChat

Migration Note: Migrated from platform channels to native Dart implementation. Behavior and signature remain identical for backward compatibility.

Android Reference: CometChat.logout(CallbackListener)

Implementation

static Future<void> logout(
    {required Function(String message)? onSuccess,
    required Function(CometChatException excep)? onError}) async {
  try {
    // Get SDK instance
    final sdk = SdkRegistry.getInstance();

    // Cancel stream subscriptions before logout
    _cancelStreamSubscriptions();

    // Call native Dart auth repository
    await sdk.auth.logout();

    // Notify login listeners
    _loginListeners.values.forEach((element) {
      element.logoutSuccess();
    });

    // Call success callback
    if (onSuccess != null) onSuccess('Logout successful');
  } on SdkException catch (sdkEx) {
    // Convert SdkException to CometChatException
    final cometChatEx = CometChatException(
      sdkEx.code,
      sdkEx.details ?? sdkEx.message,
      sdkEx.message,
    );

    // Notify login listeners
    _loginListeners.values.forEach((element) {
      element.logoutFailure(cometChatEx);
    });

    _errorCallbackHandler(cometChatEx, null, null, onError);
  } catch (e) {
    // Handle unexpected errors
    final cometChatEx = CometChatException(
      ErrorCode.errorUnhandledException,
      e.toString(),
      e.toString(),
    );

    // Notify login listeners
    _loginListeners.values.forEach((element) {
      element.logoutFailure(cometChatEx);
    });

    _errorCallbackHandler(null, null, e, onError);
  }
}