Using stream controllers and sinks
StreamControllers
create a linked Stream
and Sink
. While streams contain data emitted sequentially that can be received by any subscriber, sinks
are used to insert events.
StreamControllers
simplify stream management, automatically creating a stream and a sink, and methods to control their events and features.
In this recipe, you will create a stream controller to listen to and insert new events. This will show you how to fully control a stream.
Getting ready
In order to follow along with this recipe, you should have completed the code in the previous recipe, How to use Dart streams.
How to do it...
For this recipe, we will make our app display a random number on the screen, leveraging StreamControllers
and their sink
property:
- In the
stream.dart
file, import thedart:async
library:import 'dart:async';
- At the bottom of the
stream.dart
file, add a new class, calledNumberStream
...