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?