initWithConfig static method

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

Initialize the SDK This should only be called once returns the error or success message

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';
    log(msg, logLevel: LogLevel.ERROR);
    return msg;
  }
  if (config.serverURL.isEmpty) {
    String msg = 'initWithConfig, serverURL cannot be empty';
    log(msg, logLevel: LogLevel.ERROR);
    return msg;
  }
  if (config.appKey.isEmpty) {
    String msg = 'initWithConfig, appKey cannot be empty';
    log(msg, logLevel: LogLevel.ERROR);
    return msg;
  }
  if (config.manualSessionEnabled != null) {
    _manualSessionControlEnabled = config.manualSessionEnabled!;
    if (config.manualSessionEnabled!) {
      _instance._sessionsInternal.enableManualSession();
    }
  }
  if (config.enableUnhandledCrashReporting != null) {
    // To catch all errors thrown within the flutter framework, we use
    FlutterError.onError = _recordFlutterError;

    // Asynchronous errors are not caught by the Flutter framework. For example
    // ElevatedButton(
    //   onPressed: () async {
    //     throw Error();
    //   }
    // )
    // To catch such errors, we use
    PlatformDispatcher.instance.onError = (e, s) {
      _internalRecordError(e, s);
      return true;
    };

    _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);
  }

  if (config.content.contentCallback != null) {
    log('[initWithConfig] About to register content callback', logLevel: LogLevel.VERBOSE);
    Countly.instance._contentBuilderInternal.registerContentCallback(config.content.contentCallback!);
  }

  return result;
}