initialize static method

Future<void> initialize({
  1. String? developmentUrl,
  2. String? productionUrl,
  3. String? stagingUrl,
})

Initialize the service with SharedPreferences

Implementation

static Future<void> initialize({
  String? developmentUrl,
  String? productionUrl,
  String? stagingUrl,
}) async {
  _prefs ??= await SharedPreferences.getInstance();

  // Save user selection state before clearing
  final savedUserSelection = _prefs?.getBool(_hasUserSelectedKey) ?? false;
  final savedEnvName = _prefs?.getString(_currentEnvKey);

  // If custom URLs are provided, clear existing environments and add them
  if (developmentUrl != null || productionUrl != null || stagingUrl != null) {
    // Clear environments but preserve user selection
    await _prefs?.remove(_environmentsKey);
    instance.environments.clear();

    // Restore user selection state after clearing
    if (savedUserSelection && savedEnvName != null) {
      await _prefs?.setBool(_hasUserSelectedKey, savedUserSelection);
      await _prefs?.setString(_currentEnvKey, savedEnvName);
    }

    await _setupCustomEnvironments(
      developmentUrl: developmentUrl,
      productionUrl: productionUrl,
      stagingUrl: stagingUrl,
    );
  }
}