fetchDataAndEmitState method

Future<void> fetchDataAndEmitState()

Fetch the data from the given method and emit the specific state

Emit MslFetcherProviderLoading while the data is being fetched Emit MslFetcherProviderDataAvailable if the data is available Emit MslFetcherProviderError if there is an error within the process

Implementation

Future<void> fetchDataAndEmitState() async {
  emit(MslFetcherProviderLoading());

  try {
    T data = await onDataFetched();
    emit(
      MslFetcherProviderDataAvailable(
        data: data,
      ),
    );
  } catch (e) {
    if (showErrorLogs != null && showErrorLogs!) {
      log('MslFetchProvider<${T.toString()}> Exception: ${e.toString()}');
    }

    emit(
      MslFetcherProviderError(
        error: e,
      ),
    );
  }
}