setStorageItem function

Future<void> setStorageItem(
  1. String name,
  2. dynamic value, {
  3. int? days,
})

Set a storage item (replaces cookie functionality) name - Storage key name value - Value to store days - Number of days to persist (null for session-only)

Implementation

Future<void> setStorageItem(String name, dynamic value, {int? days}) async {
  try {
    final prefs = await SharedPreferences.getInstance();
    final data = {
      'value': value.toString(),
      'expires': days != null ? DateTime.now().millisecondsSinceEpoch + (days * 24 * 60 * 60 * 1000) : null,
    };
    await prefs.setString(name, jsonEncode(data));
  } catch (e) {
    // Handle error silently
  }
}