0

I have the following situation, I am fetching cryptocurrencies from API:

class CryptocurrenciesBloc
    extends Bloc<CryptocurrenciesEvent, CryptocurrenciesState> {
  final CryptocurrencyRepository _cryptocurrencyRepository;

  CryptocurrenciesBloc(this._cryptocurrencyRepository)
      : super(CryptocurrenciesLoadInProgress()) {
    on<CryptocurrenciesFetched>(_onCryptocurrenciesFetched);
  }

  void _onCryptocurrenciesFetched(
      CryptocurrenciesFetched event, Emitter<CryptocurrenciesState> emit) async {
    try {
      final List<PopularCryptocurrency> trendingBasicInformations =
          await _cryptocurrencyRepository.fetchTrending();

      final List<String> ids = <String>[];

      for (final singleBasicInformation in trendingBasicInformations) {
        ids.add(singleBasicInformation.id);
      }

      final List<List<CryptocurrencyResponse>> cryptocurrencies =
          await Future.wait([
        _cryptocurrencyRepository.fetchCryptocurrencies(),
        _cryptocurrencyRepository.fetchCryptocurrenciesByIds(ids)
      ]);

      emit(CryptocurrenciesLoadSuccess(
          cryptocurrencies[0], cryptocurrencies[1]));
    } on Exception {
      emit(CryptocurrenciesLoadFailure());
    }
  }
}

After the first cryptocurrency fetch, I would like to get new values (cryptocurrencies) ​​every X seconds. How can I do this using BloC?

Newbie
  • 27
  • 1
  • 5
  • Use the Time class or Stream.periodic see this answer https://stackoverflow.com/questions/14946012/how-do-i-run-a-reoccurring-function-in-dart – Ridha Rezzag Jan 24 '22 at 18:09

0 Answers0