Skip to content

Instantly share code, notes, and snippets.

@voratham
Last active April 29, 2021 17:30
Show Gist options
  • Save voratham/ab7444dd71a6a95c1e578cb9df18edee to your computer and use it in GitHub Desktop.
Save voratham/ab7444dd71a6a95c1e578cb9df18edee to your computer and use it in GitHub Desktop.
Dart Example with stream
import "dart:html";
import "dart:async";
void main() {
final InputElement input = querySelector("input");
final DivElement div = querySelector("div");
final validatorEmailFormatter =
new StreamTransformer.fromHandlers(handleData: (inputValue, sink) {
if (inputValue.contains("@")) {
sink.add(inputValue);
} else {
sink.addError("Enter a valid emial!");
}
});
final validatorNotGmailAddress =
new StreamTransformer.fromHandlers(handleData: (inputValue, sink) {
if (inputValue.contains("gmail.")) {
sink.add(inputValue);
} else {
sink.addError("You must used gmail only !! 😡");
}
});
input.onInput
.map((dynamic event) => event.target.value)
.transform(validatorEmailFormatter)
.transform(validatorNotGmailAddress)
.listen((input) {
print("Email is valid!! \"$input\" ");
div.innerHtml = "";
},
onDone: () => print("Done stream."),
onError: (error) => div.innerHtml = error);
}
import "dart:async";
class Cake {}
class Order {
String type;
Order(this.type);
}
void main() {
final controller = new StreamController();
final order = new Order("banana");
final order2 = new Order("chocolate");
final baker =
new StreamTransformer.fromHandlers(handleData: (cakeType, sink) {
if (cakeType == "chocolate") {
sink.add(new Cake());
} else {
sink.addError('I cant bake that type!!');
}
});
controller.sink.add(order);
controller.sink.add(order2);
controller.stream
.map((order) => order.type)
.transform(baker)
.listen((cake) => print("Heres your cake $cake"))
.onError((error) => print(error));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment