write method

  1. @override
FutureOr<void> write(
  1. String key,
  2. String value,
  3. StorageOptions options
)

Writes value associated with key.

This should create a new entry if key does not exist, or update the existing entry if it does.

Implementation

@override
FutureOr<void> write(String key, String value, StorageOptions options) {
  if (key.endsWith(":expire_at") || key.endsWith(":destroy")) {
    throw ArgumentError.value(key, 'key', 'Cannot use reserved keys');
  }

  _preferences.setString("$_prefix:$key", value);

  if (options.cacheTime.duration case Duration duration) {
    _preferences.setString(
      "$_prefix:$key:expire_at",
      DateTime.now().add(duration).toIso8601String(),
    );
  } else {
    _preferences.remove("$_prefix:$key:expire_at");
  }

  if (options.destroyKey != null) {
    _preferences.setString(
      "$_prefix:$key:destroy",
      options.destroyKey!,
    );
  } else {
    _preferences.remove("$_prefix:$key:destroy");
  }
}