get<T> method

Future<T?> get<T>(
  1. String key
)

Retrieves and decodes a stored JSON object. Returns null if absent or expired.

Example:

final data = await storage.get<Map<String, dynamic>>('cart');

Implementation

Future<T?> get<T>(String key) async {
  final serialized = await adapter.read(key);
  if (serialized == null) return null;

  try {
    final decoded = jsonDecode(serialized) as Map<String, dynamic>;
    final expiresAtStr = decoded['expiresAt'] as String?;
    if (expiresAtStr != null) {
      final expiresAt = DateTime.parse(expiresAtStr);
      if (DateTime.now().isAfter(expiresAt)) {
        logger.debug('BloomJsonStorage: Expired entry for key "$key". Removing...');
        await adapter.delete(key);
        return null;
      }
    }
    return decoded['data'] as T?;
  } catch (e) {
    logger.error('BloomJsonStorage: Failed to decode value for key "$key": $e');
    return null;
  }
}