我是Rxdart的新手,我已经尝试过将Combinelatest2用于2种流,例如:
Observable<String> get email => _user.stream.transform(validateEmail);
Observable<String> get password => _password.stream.transform(validatePassword);
Observable<bool> get submitValid => Observable.combineLatest2(email, password,
(checkEmail, checkPassword) => true);
在我的validateEmail和validatePassword中:
final validateEmail= StreamTransformer<String, String>.fromHandlers(
handleData: (user, sink){
if(EmailValidator.validate(email)){
sink.add(email);
}else{
sink.addError("email wrong Format!!");
}
}
);
final validatePassword = StreamTransformer<String, String>.fromHandlers(
handleData: (password, sink){
if(password.length > 2){
sink.add(password);
}else{
sink.addError("Password must be at least 3 characters");
}
}
);
这是我主页屏幕上的按钮:
Widget submitButton(ChangePasswordBloc bloc){
return StreamBuilder(
// stream: bloc.submitValid,
builder: (context, snapShot){
return RaisedButton(
child: Text("Change Password"),
color: Colors.blue[400],
onPressed: () {
if(snapShot.hasData){
}else{
return null;
}
},
);
},
);
}
如果使用combinatelatest2,我可以观察到两种发射的变化,并且可以打开/关闭我的按钮,但是如果我只想观察电子邮件,该怎么办?如果电子邮件发送者是正确的,则提交按钮处于打开状态,反之亦然
答案 0 :(得分:1)
您可以使用地图来做到这一点。除了映射电子邮件和密码,您还可以映射电子邮件并返回布尔值。
Observable<bool> get submitValid => email.map((email) => true);