RxDart。 BehaviorSubject 的侦听器在 subject.add(value) 之后没有获取值
RxDart. Listener of BehaviorSubject does not get value after subject.add(value)
我有两个 类:CurrencyRepo 和 CurrencyFetcher。从 CurrencyFetcher 中,我尝试收听存储库中的 behaviorSubject。但是当我在 repo 内的 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);
我有两个 类:CurrencyRepo 和 CurrencyFetcher。从 CurrencyFetcher 中,我尝试收听存储库中的 behaviorSubject。但是当我在 repo 内的 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);