What is the difference between Future and Stream?
A Future completes once with a value. A Stream can emit multiple values over time.
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
StreamBuilder works similarly to FutureBuilder, but instead of waiting for a single value, it listens to a Stream that can emit many values over time. Every time the Stream sends a new event, the builder function runs again with the latest data. This makes it perfect for real-time features like chat messages, live scores, stock prices, or any data that updates continuously without the user refreshing the page.
1// Example: Real-time counter2StreamBuilder<int>(3stream: counterStream, // Stream<int>4initialData: 0, // Starting value5builder: (context, snapshot) {6if (snapshot.hasError) {7return Text('Error: ${snapshot.error}');8}910return Text('Count: ${snapshot.data}');11},12)1314// Creating a simple stream15Stream<int> counterStream = Stream.periodic(16Duration(seconds: 1),17(count) => count,18);
One of the most common uses of StreamBuilder is with Firebase Firestore. When you call .snapshots() on a Firestore collection, it returns a Stream that automatically emits a new snapshot every time any document in that collection changes. This means your UI updates instantly when data is added, modified, or deleted on the server.
1// Firestore real-time updates2StreamBuilder<QuerySnapshot>(3stream: FirebaseFirestore.instance4.collection('messages')5.orderBy('timestamp')6.snapshots(),7builder: (context, snapshot) {8if (snapshot.connectionState == ConnectionState.waiting) {9return CircularProgressIndicator();10}1112if (!snapshot.hasData || snapshot.data!.docs.isEmpty) {13return Text('No messages yet');14}1516final messages = snapshot.data!.docs;17return ListView.builder(18itemCount: messages.length,19itemBuilder: (context, index) {20final message = messages[index].data() as Map;21return ListTile(22title: Text(message['text']),23subtitle: Text(message['sender']),24);25},26);27},28)
Use FutureBuilder for one-time async operations (API calls). Use StreamBuilder for continuous updates (real-time data, websockets).
Stream UIs should prioritize continuity:
Real-time data is valuable only when UI remains calm and readable.
Handle real time data streams. Dart is the language behind Flutter, and clear Dart code makes your Flutter UI easier to maintain.
Connect each concept to real app features you can build.
Prefer readable names, explicit intent, and async/await over deeply nested callbacks.
When Dart basics are weak, state bugs, null crashes, and async confusion increase quickly.
A Future completes once with a value. A Stream can emit multiple values over time.
initialData gives the builder an initial value to display while waiting for the first stream event.
Firestore's snapshots() method returns a Stream that emits whenever data changes in real-time.
Stream.periodic creates a stream that emits events at regular time intervals.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue