我的目标是使用多个流而不必嵌套 3+ StreamBuilders。
我已经在使用 rxdart 并且我研究了 mergewith 但我真的不明白使用它的正确语法?
我目前有一个名为InfoBloc
. 这是里面的代码InfoBloc
final _name = BehaviorSubject<String>();
final _description = BehaviorSubject<String>();
final _picture = BehaviorSubject<File>();
Observable<String> get name => _name.stream.transform(_validateName);
Observable<File> get picture => _picture.stream;
Observable<String> get eventDescription =>
_description.stream.transform(_validateMessage);
final _validateMessage = StreamTransformer<String, String>.fromHandlers(
handleData: (eventMessage, sink) {
if (eventMessage.length > 0) {
sink.add(eventMessage);
} else {
sink.addError(StringConstant.eventValidateMessage);
}
});
var obs = Observable.merge([])
final _validateName = StreamTransformer<String, String>.fromHandlers(
handleData: (String name, sink) {
if (RegExp(r'[!@#<>?":_`~;[\]\\|=+)(*&^%0-9-]').hasMatch(name)) {
sink.addError(StringConstant.nameValidateMessage);
} else {
sink.add(name);
}
});
void dispose() async {
await _description.drain();
_description.close();
await _name.drain();
_name.close();
await _picture.drain();
_picture.close();
}
现在在一个小部件内部,我需要名称、图片和描述快照。所以我通常会这样做
StreamBuilder(
stream: _bloc.name,
builder: (BuildContext context, AsyncSnapshot<String> snapshotName) {
return StreamBuilder(
stream: _bloc.eventDescription,
builder: (BuildContext context, AsyncSnapshot<String> snapshotDescription) {
return StreamBuilder(
stream: _bloc.picture,
builder: (BuildContext context, AsyncSnapshot<File> snapshotName) {
但是必须有更好的方法来做到这一点。
梦想是我可以在InfoBloc
文件中制作一些可以组合所有这些流的东西,并且我只需要使用一次 StreamBuilder 来流化组合的流。