ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
Stream in Dart represents a sequence of asynchronous events. While an Iterable provides synchronous delivery of data, a Stream delivers data, errors, and a completion signal asynchronously over time. It is a core primitive of Dart’s asynchronous programming model, implementing the reactive programming pattern.
Event Types
A stream communicates with its subscribers by emitting three distinct types of events:- Data Events: The actual payload of type
T. - Error Events: Exceptions or errors that occur during stream processing.
- Done Event: A terminal signal indicating no further data or error events will be emitted.
Stream Architectures
Dart categorizes streams into two architectural models based on subscription behavior:- Single-subscription Streams: The default stream type. It permits only one listener throughout its lifecycle. Attempting to listen a second time throws a
StateError. The behavior of the stream before a subscription occurs depends on its instantiation: streams generated viaasync*are lazy and do not generate events until a listener attaches, whereas streams created manually via aStreamControllerwill buffer emitted events internally to ensure no data is lost before the subscription occurs. - Broadcast Streams: Designed for multiple concurrent listeners. It does not buffer events; if an event is emitted when no listeners are attached, the event is permanently discarded. Listeners only receive events emitted after they subscribe.
Consuming Streams
Developers can consume streams using either asynchronous iteration or the subscription API. Asynchronous Iteration (await for)
This syntax pauses execution of the surrounding async function until the stream emits a new event or completes.
listen)
The listen method returns a StreamSubscription<T>, providing granular control over the stream state (pause, resume, cancel) and explicit handlers for different event types.
Creating Streams
Streams are typically instantiated via asynchronous generator functions or manual controllers. Asynchronous Generators (async*)
The async* keyword defines a generator function that returns a Stream. The yield keyword emits individual data events, while yield* delegates to another stream.
StreamController provides an imperative API to construct and manage a stream. It exposes a sink for event injection and a stream for subscription.
Stream Transformations
Streams support functional composition. Methods likemap, where, and expand return new streams that intercept and mutate events from the source stream before they reach the listener. For complex transformations, the transform method applies a StreamTransformer.
Master Dart with Deep Grasping Methodology!Learn More





