init static method

Future<void> init(
  1. String appId,
  2. AppSettings appSettings, {
  3. required dynamic onSuccess(
    1. String successMessage
    )?,
  4. required dynamic onError(
    1. CometChatException e
    )?,
})

method initializes the settings required for CometChat

We suggest you call the init() method on app startup

appId refers to your CometChat App ID. region The region where the app was created.

Implementation

static Future<void> init(String appId, AppSettings appSettings, {required Function(String successMessage)? onSuccess, required Function(CometChatException e)? onError}) async {
  try {
    final arguments = {
      'appId': appId,
      'roles': appSettings.roles,
      'region': appSettings.region,
      'adminHost': appSettings.adminHost,
      'clientHost': appSettings.clientHost,
      'subscriptionType': appSettings.subscriptionType,
      'autoEstablishSocketConnection': appSettings.autoEstablishSocketConnection,
      'sdkVersion': PackageConstants.version,
      'platform': PackageConstants.platform
    };
    final result = await channel.invokeMethod('init', arguments);
    initializetestmessageStream();
    nativeStream?.listen((event) {});
    //connectionReceiveStreamController = StreamController<BaseMessage>.broadcast();
    if(onSuccess != null) onSuccess(result);
  } 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()));
  }
}