Skip to content

Instantly share code, notes, and snippets.

@GIfatahTH
Created October 17, 2019 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GIfatahTH/5a8097f5ffb626dcc4d30861681a0844 to your computer and use it in GitHub Desktop.
Save GIfatahTH/5a8097f5ffb626dcc4d30861681a0844 to your computer and use it in GitHub Desktop.
import 'dart:async';
class LoginFormModel {
final emailController = StreamController<String>();
final passwordController = StreamController<String>();
Function(String) get changeEmail => emailController.sink.add;
Function(String) get changePassword => passwordController.sink.add;
Stream<String> get email => emailController.stream.transform(validateEmail);
Stream<String> get password =>
passwordController.stream.transform(validatePassword);
submit() {
print("form submitted");
}
//The validation of the email. It must contain @
final validateEmail = StreamTransformer<String, String>.fromHandlers(
handleData: (email, sink) {
if (email.contains("@")) {
sink.add(email);
} else {
sink.addError("Enter a valid Email");
}
},
);
//The validation of the password. It must have more than three characters
final validatePassword = StreamTransformer<String, String>.fromHandlers(
handleData: (password, sink) {
if (password.length > 3) {
sink.add(password);
} else {
sink.addError("Enter a valid password");
}
},
);
//Disposing the controllers
dispose() {
emailController.close();
passwordController.close();
print("controllers are disposed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment