Search code examples
flutterblocrxdart

Is there function similar to CombineLatest which only for 1 stream only?


I'm new in Rxdart, and I have tried the Combinelatest2 for 2 kind of streams, for example:

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);

in my validateEmail and 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");
        }
      }
  );

and this is the button from my home page screens:

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;
            }
          },
        );
      },
    );
  }

if using the combinelatest2 i can got the change of 2 kind emission from observable and i can make my button on / off, but if i only want to observe email only, how can i do it? if the emitter from email is correct then the button of submit is on or vice-versa


Solution

  • You can use a map to do that. Instead of combining email and password, you can map email and return a boolean.

    Observable<bool> get submitValid => email.map((email) => true);