setItem method

Future<void> setItem(
  1. String key,
  2. dynamic value, [
  3. Object toEncodable(
    1. Object nonEncodable
    )?
])

Saves item by key to a storage. Value should be json encodable (json.encode() is called under the hood). After item was set to storage, consecutive getItem will return json representation of this item if toEncodable is provided, it is called before setting item to storage otherwise value.toJson() is called

Implementation

Future<void> setItem(
  String key,
  value, [
  Object toEncodable(Object nonEncodable)?,
]) async {
  var data = toEncodable?.call(value) ?? null;
  if (data == null) {
    try {
      data = value.toJson();
    } on NoSuchMethodError catch (_) {
      data = value;
    }
  }

  await _dir.setItem(key, data);

  return _flush();
}