write method

Future<void> write(
  1. String key,
  2. dynamic value
)

Implementation

Future<void> write(String key, dynamic value) async {
  final prefixedKey = '${StorageConfig.prefsPrefix}$key';

  try {
    // Detección automática de tipo
    if (value is bool) {
      await _prefs.setBool(prefixedKey, value);
    } else if (value is int) {
      await _prefs.setInt(prefixedKey, value);
    } else if (value is double) {
      await _prefs.setDouble(prefixedKey, value);
    } else if (value is String) {
      await _prefs.setString(prefixedKey, value);
    } else if (value is List<String>) {
      await _prefs.setStringList(prefixedKey, value);
    } else {
      // Objetos complejos con codificación de Timestamps
      final String encodedValue = jsonEncode(value, toEncodable: customJsonEncoder);
      await _prefs.setString(prefixedKey, encodedValue);
    }

    final currentData = Map<String, dynamic>.from(_dataSubject.value);
    currentData[key] = value;
    _dataSubject.add(currentData);
    _updateSubject.add(key);

  } catch (e) {
    throw StorageException(
      'Error escribiendo preferencia',
      details: e.toString(),
      type: StorageType.prefs,
    );
  }
}