write<T> method

Future<void> write<T>({
  1. required String key,
  2. required T? value,
})

Encrypts the given value with the encryption key associated with key

If the key was already in the storage, its associated value is changed. If the value is null, deletes associated value for the given key. Supports String and Uint8List values.

Implementation

Future<void> write<T>({
  required String key,
  required T? value,
}) async {
  assert(key.isNotEmpty, 'key must not be empty');
  await _synchronized(key, () async {
    if (value == null) return delete(key: key);
    assert(
        T == String || T == Uint8List, 'value must be String or Uint8List');
    final convertedValue = (value is String)
        ? Uint8List.fromList(utf8.encode(value))
        : value as Uint8List;
    await performWrite(key: key, value: convertedValue);
    await _getKeys();
    _keys.add(key);
    await _updateKeys();
  });
}