我正在使用RxDart观察更改并相应地更新UI。应用启动时,我正在进行网络呼叫并成功获取数据,请观察更改并相应地更新UI。但是当我在关闭屏幕的同时处理sed -E '/\.c|\.h/!d' file
时。其产生以下错误:
Subjects
这是集团集团:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (15524): The following StateError was thrown while finalizing the widget tree:
I/flutter (15524): Bad state: You cannot close the subject while items are being added from addStream
这是我称之为的UI屏幕:
class MovieDetailBloc {
final _repository = Repository();
final _movieId = PublishSubject<int>();
final _trailers = BehaviorSubject<Future<TrailerModel>>();
Function(int) get fetchTrailersById => _movieId.sink.add;
Observable<Future<TrailerModel>> get movieTrailers => _trailers.stream;
MovieDetailBloc() {
_movieId.stream.transform(_itemTransformer()).pipe(_trailers);
}
dispose() {
_movieId.close();
_trailers.close();
}
_itemTransformer() {
return ScanStreamTransformer(
(Future<TrailerModel> trailer, int id, int index) {
print(index);
trailer = _repository.fetchTrailers(id);
return trailer;
},
);
}
}
我关闭屏幕时出现错误。 dispose方法将引发以上异常。我该如何解决这个问题?
答案 0 :(得分:6)
我认为排干流应该可以解决问题
dispose() async {
_movieId.close();
await _trailers.drain();
_trailers.close();
}
https://api.dartlang.org/stable/2.0.0/dart-async/Stream-class.html
答案 1 :(得分:1)
您还可以解决替换问题:
MovieDetailBloc() {
_movieId.stream.transform(_itemTransformer()).pipe(_trailers);
}
使用
MovieDetailBloc() {
_movieId.stream.transform(_itemTransformer()).listen((data) {
if (!_categories.isClosed) {
_trailers.add(data);
}
});
}