initWithConfig static method

Future<String?> initWithConfig(
  1. CountlyConfig config
)

Implementation

static Future<String?> initWithConfig(CountlyConfig config) async {
  if (config.loggingEnabled != null) {
    _isDebug = config.loggingEnabled!;
  }
  log('Calling "initWithConfig"');
  if (_instance._countlyState.isInitialized) {
    String msg = 'initWithConfig, SDK is already initialized';
    Countly.log(msg, logLevel: LogLevel.ERROR);
    return msg;
  }
  if (config.serverURL.isEmpty) {
    String msg = 'initWithConfig, serverURL cannot be empty';
    Countly.log(msg, logLevel: LogLevel.ERROR);
    return msg;
  }
  if (config.appKey.isEmpty) {
    String msg = 'initWithConfig, appKey cannot be empty';
    Countly.log(msg, logLevel: LogLevel.ERROR);
    return msg;
  }
  if (config.manualSessionEnabled != null) {
    _manualSessionControlEnabled = config.manualSessionEnabled!;
    if (config.manualSessionEnabled!) {
      _instance._sessionsInternal.enableManualSession();
    }
  }
  if (config.enableUnhandledCrashReporting != null) {
    _enableCrashReportingFlag = config.enableUnhandledCrashReporting!;
  }
  _channel.setMethodCallHandler(_methodCallHandler);

  List<dynamic> args = [];
  args.add(_configToJson(config));

  final String? result = await _channel.invokeMethod('init', <String, dynamic>{'data': json.encode(args)});
  _instance._countlyState.isInitialized = true;

  if (config.remoteConfigGlobalCallbacks.isNotEmpty) {
    log('[initWithConfig] About to register ${config.remoteConfigGlobalCallbacks.length} callbacks', logLevel: LogLevel.VERBOSE);
  }
  for (final callback in config.remoteConfigGlobalCallbacks) {
    Countly.instance._remoteConfigInternal.registerDownloadCallback(callback);
  }

  return result;
}