我有两个类:CurrencyRepo和CurrencyFetcher。从CurrencyFetcher,我尝试收听回购中的behaviorSubject。但是,当我在回购内的behaviorSubject中添加值时,CurrencyFetcher无法获取这些值。我究竟做错了什么?
class CurrencyFetcher implements CurrencyFetcherService {
final CurrencyRepo _currencyRepo;
StreamSubscription currencySubscription;
CurrencyFetcher(this._currencyRepo, this._preferencesService) {
_subscribeToCurrencies();
}
void _subscribeToCurrencies() {
currencySubscription = _currencyRepo
.getCurrenciesStream()
.listen((currencies) => _handleApiCurrencies);
}
Future<void> _handleApiCurrencies(List<ApiCurrency> apiCurrencies) async {
// implemetation
}
}
class CurrencyRepo {
final CurrencyApi _currencyApi;
final BehaviorSubject<List<ApiCurrency>> _currencySubject = BehaviorSubject.seeded([]);
Stream<List<ApiCurrency>> getCurrenciesStream() {
_updateCurrencies();
return _currencySubject.stream;
}
CurrencyRepo(this._currencyApi);
void _updateCurrencies() {
_currencyApi.getCurrencies().then((currencies) {
_currencySubject.add(currencies);
});
}
}
我检查了CurrencyFetcher开始侦听后是否将值添加到了流中。我检查了一下,当我向流添加新值时,它具有一个侦听器。第一次使用RxDart,有人可以帮忙吗? :) 最佳答案
在函数_subscribeToCurrencies()
里面尝试写
_currencyRepo.currenciesStream.listen((currencies) {
_handleApiCurrencies(currencies);
});
代替_currencyRepo.currenciesStream.listen(_handleApiCurrencies);
关于flutter - RxDart。 BehaviorSubject的监听器在subject.add(value)之后不获取值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63504577/