update method

Future<void> update(
  1. Data resolver(
    1. Data? data
    )
)

Updates the current data and refresh the repository. The resolver function takes the current data and returns the new data. The new data will be added to the stream and the repository will be refreshed. This method is useful if you want to use Optimistic UI. You can update the data to the repository and refresh in a row.

Implementation

Future<void> update(Data Function(Data? data) resolver) async {
  // Call the resolver function to get the new data.
  final newData = resolver.call(currentValue);
  // Add the new data to the repository without refreshing yet.
  await emit(
    data: newData,
    datasource: RepositoryDatasource.optimistic,
  );
  // It's just 'cause `this` is a getter, so the 'if' below will not work
  // if we don't use it as a local variable.
  final self = this;

  if (self is PropagatingRepositoryMixin<Data>) {
    await self.propagate(newData);
  }

  // Refresh the repository.
  await refresh();
}