update<T extends Object> method

  1. @experimental
  2. @nonVirtual
  3. @override
Future<void> update<T extends Object>({
  1. required Key key,
  2. required Decoder<T?> decoder,
  3. required Transformer<T?> transformer,
  4. required Encoder<T?> encoder,
  5. Options? options,
})
override

Read–modify–write.

Updates the data transactionally in an atomic read-modify-write operation. All operations are serialized, and the transformer can perform asynchronous computations such as RPCs, database queries, API calls, etc.

The future completes when the data has been persisted durably to disk. If the transform or write to disk fails, the transaction is aborted and the error is rethrown.

When calling this, logic will be executed in the following order:

  • Read raw value by key, then decode it with decoder.
  • Transform the decoded value with transformer.
  • Encode the transformed value with encoder.
  • Finally, save encoded value to persistent storage.

Implementation

@experimental
@nonVirtual
@override
Future<void> update<T extends Object>({
  required Key key,
  required Decoder<T?> decoder,
  required Transformer<T?> transformer,
  required Encoder<T?> encoder,
  Options? options,
}) {
  assert(_debugAssertNotDisposed());

  return _enqueueWritingTask<void>(
    key,
    () async {
      // Read
      final value = await read<T>(key, decoder, options);

      // Modify
      final futureOr = transformer(value);
      final transformed = futureOr is Future<T?> ? await futureOr : futureOr;

      // Write
      await _writeWithoutSynchronization(key, transformed, encoder, options);
    },
  );
}