Subscribing to stream events
In the previous recipes of this chapter, we used the listen method to get values from a stream. This generates a Subscription. Subscriptions contain methods that allow you to listen to events from streams in a structured way.
In this recipe, we will use Subscription to gracefully handle events and errors, and close the subscription.
Getting ready
In order to follow along with this recipe, you should have completed the code in the previous recipe, Injecting data transforms into streams.
How to do it...
For this recipe, we will use a StreamSubscription with its methods. We will also add a button to close the stream. Perform the following steps:
- At the top of the
_StreamHomePageStateclass, declare aStreamSubscriptioncalledsubscription:late StreamSubscription subscription; - In the
initStatemethod of the_StreamHomePageStateclass, removeStreamTransformerand set the subscription. The final result...