Skip to content

Instantly share code, notes, and snippets.

@miquelbeltran
Last active November 22, 2019 15:48
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 miquelbeltran/f4c780239a1438505ddc7e9404b513ed to your computer and use it in GitHub Desktop.
Save miquelbeltran/f4c780239a1438505ddc7e9404b513ed to your computer and use it in GitHub Desktop.
Exercise: Listening to events
Did you remember to call listen() on the stream?
Did you call to processValue inside listen()?
import 'dart:async';
void listenToStream(Stream<String> stream) {
// implement this method
}
void listenToStream(Stream<String> stream) {
stream.listen((event) => processValue(event));
}
var processWasCalled = false;
void processValue(String value) {
print(value);
processWasCalled = value == "event";
}
void main() {
// ignore: close_sinks
final controller = StreamController<String>(sync: true);
final stream = controller.stream;
listenToStream(stream);
// Check if student used `listen`
if (!controller.hasListener) {
_result(false, ['Something went wrong! Stream has no listeners. Did you call to `stream.listen()`?']);
return;
}
controller.add("event");
// Check if student called to processValue inside listen
if (processWasCalled) {
_result(true);
} else {
_result(false, ['Something whent wrong! `processValue()` was not called when emitting a value']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment