It is often useful to manipulate values when listening to a stream. In Dart, StreamTransformer is an object that performs data transformations on a stream, so that the listeners of the stream then receive the transformed data. In the code in this recipe, you transformed the random number emitted by the stream by multiplying it by 10.
The first step was to create a stream transformer, using 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());
With the StreamTransformer.fromHandlers constructor, you specify callback functions with three named parameters: handleData, handleError, and handleDone.
handleData receives data events emitted from the stream. This is where you apply the transformation you need to perform. The function you...