refresh<StateT> method

StateT refresh<StateT>(
  1. Refreshable<StateT> refreshable
)

Forces a provider to re-evaluate its state immediately, and return the created value.

Writing:

final newValue = ref.refresh(provider);

is strictly identical to doing:

ref.invalidate(provider);
final newValue = ref.read(provider);

If you do not care about the return value of refresh, use invalidate instead. Doing so has the benefit of:

  • making the invalidation logic more resilient by avoiding multiple refreshes at once.
  • possibly avoids recomputing a provider if it isn't needed immediately.

This method is useful for features like "pull to refresh" or "retry on error", to restart a specific provider.

Implementation

StateT refresh<StateT>(Refreshable<StateT> refreshable) {
  final providerToRefresh = switch (refreshable) {
    final $ProviderBaseImpl<Object?> p => p,
    _ProviderRefreshable<Object?, Object?>(:final provider) => provider,
  };
  invalidate(providerToRefresh);

  return read(refreshable);
}