Closing a stream in Dart indicates that no more values will be sent on it. This can be useful to communicate completion to the stream’s listeners.
In this example, we’ll use a jobs stream to communicate work to be done from the main() function to a worker function. When we have no more jobs for the worker, we’ll close the jobs stream.
When you run this program, you should see output similar to this:
In Dart, streams are similar to channels in other languages. The StreamController class is used to create a stream that can be written to and listened from. The Completer class is used to create a future that can be completed later, which is similar to how we used channels for synchronization in the original example.
The concept of closing a stream leads naturally to our next example: listening to streams.