load<TSearchModel> method

Future<void> load<TSearchModel>([
  1. TSearchModel? searchModel
])

Loads data and updates the state accordingly.

Implementation

Future<void> load<TSearchModel>([TSearchModel? searchModel]) async {
  // Update the search model if the current state is filterable
  if (state is AbstractItemFilterableState) {
    (state as AbstractItemFilterableState).searchModel =
        searchModel ?? (state as AbstractItemFilterableState).searchModel;
  }

  await onBeforeLoad(searchModel);

  // Set state to loading
  state.resultStatus = ResultStatus.loading;
  updateState(state.copyWith() as S);

  try {
    // Attempt to resolve data and update state
    final result = await resolveData();

    updateState(convertResultToState(result));
    await onAfterLoad(result);
  } catch (e) {
    // On error, resolve data via stream and update state for each result
    await for (final result in resolveStreamData()) {
      updateState(convertResultToState(result));
      await onAfterLoad(result);
    }
  }
}