initialize method

void initialize(
  1. MethodCall call
)

Implementation

void initialize(MethodCall call) {
  Map<Object?, Object?> args = call.arguments as Map<Object?, Object?>;
  String token = args['token'] as String;
  dynamic config = args['config'];
  Map<String, dynamic> initConfig = Map<String, dynamic>.from(config ?? {});

  final serverURL = args['serverURL'];
  if (serverURL is String && serverURL.isNotEmpty) {
    initConfig['api_host'] = serverURL;
  }

  // Handle feature flags configuration
  dynamic featureFlags = args['featureFlags'];
  if (featureFlags != null && featureFlags is Map) {
    bool enabled = featureFlags['enabled'] == true;
    if (enabled) {
      final flagsConfig = <String, dynamic>{};
      dynamic context = featureFlags['context'];
      if (context != null && context is Map && context.isNotEmpty) {
        flagsConfig['context'] = context;
      }
      final persistence =
          _flagsPersistenceFromPolicy(featureFlags['variantLookupPolicy']);
      if (persistence != null) {
        flagsConfig['persistence'] = persistence;
      }
      // Add prefetchFlags if provided, default to true
      bool prefetchFlags = featureFlags['prefetchFlags'] ?? true;
      flagsConfig['prefetchFlags'] = prefetchFlags;
      initConfig['flags'] = flagsConfig.isEmpty ? true : flagsConfig;
    }
  }

  final jsConfig = safeJsify(initConfig) as JSObject;
  // Under dart2wasm `is JSObject` is true for any non-null JS value, so a
  // string or number in `hooks` would reach setProperty and throw out of
  // init() to the caller. Check the JS type instead, and never let hook
  // installation take down initialization.
  final hooks = jsConfig.getProperty<JSAny?>('hooks'.toJS);
  final JSObject jsHooks = (hooks != null && hooks.typeofEquals('object'))
      ? hooks as JSObject
      : <String, Object?>{}.jsify() as JSObject;
  try {
    jsHooks.setProperty('on_track'.toJS, _onTrackHook);
    jsConfig.setProperty('hooks'.toJS, jsHooks);
  } catch (e) {
    developer.log(
      '`init` could not install the on_track hook: $e',
      name: 'Mixpanel',
    );
  }
  init(token, jsConfig);
}