Advanced State Management with Streams
In Dart and Flutter, futures and streams are the main tools to deal with asynchronous programming.
While Future
represents a single value that will be delivered at a later time in the future, Stream
is a set (sequence) of values (0 or more) that can be delivered asynchronously, at any time. Basically, it is a flow of continuous data.
In order to get data from a stream, you subscribe (or listen) to it. Each time some data is emitted, you can receive and manipulate it as required by the logic of your app.
By default, each stream allows only a single subscription, but you will also see how to enable multiple subscriptions.
This chapter focuses on several use cases of streams in a Flutter app. You will see streams used in different scenarios, and how to read and write data to streams, build user interfaces based on streams, and use the BLoC state management pattern.
Like futures, streams can generate data or errors...