write method

Future<T?> write(
  1. String key,
  2. T data, {
  3. bool silent = false,
  4. String? eTag,
  5. ETagReceiver? eTagReceiver,
})

Writes the given data to the store.

This method will store data unter key in the store, replacing anything stored there before. On success, the written value is returned, unless silent is set to true. In that case, it always returns null.

If eTagReceiver was specified, it will contain the current eTag of the written entry after the returned future was resolved. To only write to the store if data was not changed, pass the eTag of the last known value.

Note: When using eTag, silent must not be true as these two cannot be combined.

Implementation

Future<T?> write(
  String key,
  T data, {
  bool silent = false,
  String? eTag,
  ETagReceiver? eTagReceiver,
}) async {
  assert(
    !silent || eTag == null,
    'Cannot set silent and eTag at the same time',
  );
  final response = await restApi.put(
    dataToJson(data),
    path: _buildPath(key),
    printMode: silent ? PrintMode.silent : null,
    ifMatch: eTag,
    eTag: eTagReceiver != null,
  );
  _applyETag(eTagReceiver, response);
  return !silent && response.data != null
      ? dataFromJson(response.data)
      : null;
}