readString method

Future<String?> readString(
  1. String key, {
  2. bool encryptedValue = false,
})

Implementation

Future<String?> readString(String key, {bool encryptedValue = false}) async {
  final box = await _openBox();

  final stored = box.get(key);

  if (!encryptedValue || stored == null) {
    return stored;
  }

  try {
    final parts = stored.split(':');

    if (parts.length != 2) return null;

    final iv = encrypt.IV.fromBase64(parts[0]);
    final encrypedData = parts[1];

    final decrypted = _encrypter.decrypt64(encrypedData, iv: iv);

    return decrypted;
  } catch (e, stack) {
    debugPrint('🔒 Error decrypting: $e');
    debugPrint('$stack');
    return null;
  }
}