refresh method

  1. @override
Future<T?> refresh()
override

Refresh the Injected state. Refreshing the state means reinitialize it and reinvoke its creation function and notify its listeners.

If the state is persisted using PersistState, the state is deleted from the store and the new recalculated state is stored instead.

If the Injected model has Injected.inherited injected models, they will be refreshed also.

Implementation

@override
Future<T?> refresh() async {
  final stackTrace = kDebugMode ? StackTrace.current : null;

  if (!isInitialized) {
    //If refresh is called in non initialized state
    //then just initialize it and return
    initialize();
  } else {
    _snapState = _snapState.copyToIsIdle(
      infoMessage: kRefreshMessage,
      data: initialState,
    );
    _notifyDependentListeners();
    setStateNullable(
      (_) => mockableCreator(),
      middleSetState: middleSetCreator,
      stackTrace: stackTrace,
    );
    if (!isWaitingToInitialize) {
      notify();
    }
  }

  try {
    return await stateAsync;
  } catch (e) {
    return _snapState.data;
  }
}