For this recipe, you will edit the random number on the screen, leveraging a StreamTransformer, starting from the code that you have completed in the previous recipe, Using stream controllers and sinks:
- At the top of the _StreamHomePageState class, in the main.dart file, add a declaration of StreamTransformer:
StreamTransformer transformer;
- In the initState method, just after the declarations, create an instance of StreamTransformer, calling the fromHandlers constructor:
transformer = StreamTransformer<int, dynamic>.fromHandlers(
handleData: (value, sink) {
sink.add(value * 10);
},
handleError: (error, trace, sink) {
sink.add(-1);
},
handleDone: (sink) => sink.close());
- Still in the initState method, edit the listen method over the stream, so that you call transform over it, passing the transformer as a parameter:
stream.transform(transformer).listen((event) {
setState(()...