getString method

Future<String?> getString(
  1. String key, {
  2. String? defaultValue,
})

Retrieve a String value

Returns defaultValue (null by default) if key doesn't exist or is empty Gracefully handles decryption errors by returning empty string or defaultValue

Implementation

Future<String?> getString(String key, {String? defaultValue}) async {
  _checkInit();
  _validateKey(key);
  try {
    // Check cache first
    if (_cache.containsKey(key)) {
      final encrypted = _cache[key]!;
      if (encrypted.isEmpty) return defaultValue;
      final decrypted = _decrypt(encrypted);
      return decrypted.isEmpty ? (defaultValue ?? decrypted) : decrypted;
    }

    return await _withRetry(() async {
      final encrypted = await _secureStorage.read(key: key);
      if (encrypted == null || encrypted.isEmpty) return defaultValue;

      _updateCache(key, encrypted);
      final decrypted = _decrypt(encrypted);
      return decrypted.isEmpty ? (defaultValue ?? decrypted) : decrypted;
    });
  } catch (e) {
    return defaultValue;
  }
}