update<T extends Object> method
- @experimental
- @nonVirtual
- @override
- required Key key,
- required Decoder<
T?> decoder, - required Transformer<
T?> transformer, - required Encoder<
T?> encoder, - 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 withdecoder
. - 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);
},
);
}