refresh method

void refresh({
  1. bool force = false,
  2. bool alwaysTouch = false,
})

Refreshes this notifier with fresh Data if it is stale or forced.

Data is updated using the onFetch function. This function will be called in these conditions:

  • Data haven't been fetched yet (i.e. the notifier is in the Initial state).
  • No expiration has been set (i.e. cache never expires).
  • Data is stale (isStale) and not already loading (i.e. isLoading is false).
  • The current state is Error (but not cancelled) and willRefreshOnError is true.
  • Fetch is forced (i.e. force is true).

The refresh method is normally invoked automatically when a listener is added (see addListener), but it can also be invoked manually to request or force a refresh.

If alwaysTouch if true, the result will be touched (meaning listeners will be notified) even if the data is not stale or refresh is forced.

Implementation

void refresh({bool force = false, bool alwaysTouch = false}) {
  assert(ChangeNotifier.debugAssertNotDisposed(this));
  if (!force && // Skip fetch if not forced and...
      (isFresh && !isAlwaysFresh || // ...data is still fresh (and can expire) - or...
          isError &&
              !isCancelled &&
              !willRefreshOnError || // ...an error occurred, but refresh shouldn't be performed for errors - or...
          isLoading && !isInitial)) // ... already loading (but not Initial)
  {
    if (alwaysTouch) touch();
    return;
  }

  try {
    fetchData();
  } catch (error, stackTrace) {
    toError(error: error, stackTrace: stackTrace);
  }
}