createUser static method

Future<User?> createUser(
  1. User user,
  2. String authKey, {
  3. required dynamic onSuccess(
    1. User user
    )?,
  4. required dynamic onError(
    1. CometChatException excep
    )?,
})

Method returns user after creation in cometchat environment

Ideally, user creation should take place at your backend

uid specified on user creation. Not editable after that.

name Display name of the user.

avatar URL to profile picture of the user.

Implementation

static Future<User?> createUser(User user, String authKey, {required Function(User user)? onSuccess, required Function(CometChatException excep)? onError}) async {
  try {
    final result = await channel.invokeMethod('createUser', {
      'apiKey': authKey,
      'uid': user.uid,
      'name': user.name,
      'link': user.link,
      'tags': user.tags,
      'avatar': user.avatar,
      'statusMessage': user.statusMessage,
      'metadata': user.metadata == null ? null : json.encode(user.metadata),
      'role': user.role,
      'lastActiveAt': user.lastActiveAt == null
          ? null
          : user.lastActiveAt!.millisecondsSinceEpoch,
      'hasBlockedMe': user.hasBlockedMe,
      'blockedByMe': user.blockedByMe,
      'status': user.status
    });
    final User res = User.fromMap(result);
    if(onSuccess != null) onSuccess(res);
    return res;
  } on PlatformException catch (platformException) {
    if(onError != null) onError(CometChatException(platformException.code, platformException.details, platformException.message));
  } catch (e) {
    debugPrint("Error: $e");
    if(onError != null) onError(CometChatException(ErrorCode.errorUnhandledException, e.toString() , e.toString()));
  }
  return null;
}