getLoggedInUser static method

Future<User?> getLoggedInUser({
  1. dynamic onSuccess(
    1. User
    )?,
  2. dynamic onError(
    1. CometChatException excep
    )?,
})

Returns logged in User Object

check of getLoggedInUser() function in your app where you check App's user login status. In case it returns nil then you need to call the Login method inside it.

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

Android Reference: CometChat.getLoggedInUser()

Implementation

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

    // Call native Dart auth repository (synchronous)
    // The auth repository returns the full User object with all fields
    final user = sdk.auth.getLoggedInUser();

    // Call success callback if user exists
    if (user != null && onSuccess != null) {
      onSuccess(user);
    }

    return user;
  } on SdkException catch (sdkEx) {
    // Convert SdkException to CometChatException
    final cometChatEx = CometChatException(
      sdkEx.code,
      sdkEx.details ?? sdkEx.message,
      sdkEx.message,
    );
    _errorCallbackHandler(cometChatEx, null, null, onError);
    return null;
  } catch (e) {
    _errorCallbackHandler(null, null, e, onError);
    return null;
  }
}