set<T> method

Future<void> set<T>(
  1. String key,
  2. T value, {
  3. Duration? ttl,
  4. Duration? expiresIn,
})

Stores a JSON-encodable value with optional ttl or expiresIn duration.

Example:

await storage.set('cart', {'items': [1, 2, 3]}, ttl: const Duration(days: 7));

Implementation

Future<void> set<T>(String key, T value, {Duration? ttl, Duration? expiresIn}) async {
  final effectiveTtl = ttl ?? expiresIn;
  final now = DateTime.now();
  final wrapper = {
    'data': value,
    'savedAt': now.toIso8601String(),
    'expiresAt': effectiveTtl != null ? now.add(effectiveTtl).toIso8601String() : null,
  };
  final serialized = jsonEncode(wrapper);
  await adapter.write(key, serialized);
}