Allowing multiple stream subscriptions
By default, each stream allows only a single subscription. If you try to listen to the same subscription more than once, you get an error. Flutter also has a specific kind of stream, called a broadcast stream, which allows multiple listeners. In this recipe, you will see a simple example of using a broadcast stream.
Getting ready
In order to follow along with this recipe, you should have completed the code in the previous recipe, Subscribing to stream events.
How to do it...
For this recipe, we’ll add a second listener to the stream that we built in the previous recipes in this chapter:
- At the top of the
_StreamHomePageState
class, in themain.dart
file, declare a secondStreamSubscription
, calledsubscription2
, and a string, calledvalues
:late StreamSubscription subscription2; String values = '';
- In the
initState
method, edit the first subscription and listen to the stream with...