When you call the listen method over a stream, you get a StreamSubscription. When you create a StreamSubscription, you may set four parameters, one required, and three optional:
| Parameter | Optional/Required | Type |
| onListen | Required – Positional as first parameter | Function |
| onDone | Optional | Function |
| onError | Optional | Function |
| cancelOnError | Optional | Bool |
In the example in this recipe, we set the first parameter (onListen) when we create the StreamSubscription:
subscription = stream.listen((event) {
setState(() {
lastNumber = event;
});
});
As you have also seen in previous recipes, this callback is triggered whenever some data is emitted by the stream. For the optional parameters, we set them later through subscription properties.
In particular, we set the onError property with the help of the following command:
subscription.onError((error) {
setState(() {
lastNumber = -1;
});
});
onError gets called whenever the...