-3
class PortfolioHomeCubit extends Cubit<PortfolioHomeState> {
  PortfolioHomeCubit() : super(PortfolioHomeState());
  final apiService = ApiService();
  Future<void> _onFetchContent() async {
    final cache = apiService.cache;
    final homeContent = cache.portfolioHomeContent;
    emit(state.copyWith(portfolioHomeContent: homeContent));
    final response = await apiService.getData();
    final content = response.portfolioHomeContent;
    emit(state.copyWith(portfolioHomeContent: content));
  }
}
final portfolioHomeContentProvider =
    BlocProvider<PortfolioHomeCubit, PortfolioHomeState>(
  ((ref) => PortfolioHomeCubit().._onFetchContent()),
);

This code works just fine but I am confused. If the _onfetch function retrieves cached data from the ApiService object why do fire another API request? Is it because the cache can be null? and why will it be null when an api request is already fired in the ApiService and saved to the cache variable?

Just incase, this is river_bloc

1 Answers1

0

It fetches data both from cache and API server , and then with another function that will check if the cache is null and the internet is not available , it will return a warning in the screen with no internet error .