get() vs. snapshots()
There are two ways to read data from Firestore. collection.get() runs the query once and returns a Future<QuerySnapshot> — think of it like a single HTTP request. collection.snapshots() returns a Stream<QuerySnapshot> instead: whenever a matching document changes, is deleted, or a new one is added, Firestore automatically pushes a new snapshot. For 'must stay up to date at all times' scenarios like chat screens, live leaderboards, or notification counters, this is the right tool.
Basic Usage with StreamBuilder
StreamBuilder listens to a Stream and rebuilds the widget tree on every new value. Handling ConnectionState correctly matters for distinguishing the initial load from an error state.
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('mesajlar')
.orderBy('tarih', descending: true)
.limit(50)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text('Hata: ${snapshot.error}');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
}
final dokumanlar = snapshot.data!.docs;
return ListView.builder(
itemCount: dokumanlar.length,
itemBuilder: (context, index) {
final veri = dokumanlar[index].data() as Map<String, dynamic>;
return ListTile(title: Text(veri['metin'] ?? ''));
},
);
},
)
Combining It with Queries
where, orderBy and limit work perfectly with snapshots() — the stream only fires when documents matching the query change. For example, to listen only to unread notifications:
FirebaseFirestore.instance
.collection('bildirimler')
.where('kullaniciId', isEqualTo: kullaniciId)
.where('okundu', isEqualTo: false)
.orderBy('tarih', descending: true)
.snapshots();
Listening to a Single Document
If you want to watch a single document live (say, a user's profile or a session's status) instead of a whole collection, use doc(id).snapshots(); access it through DocumentSnapshot's exists and data().
StreamBuilder<DocumentSnapshot>(
stream: FirebaseFirestore.instance
.collection('kullanicilar')
.doc(kullaniciId)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData || !snapshot.data!.exists) {
return const SizedBox.shrink();
}
final veri = snapshot.data!.data() as Map<String, dynamic>;
return Text('Durum: ${veri['durum']}');
},
)
Offline Behavior
Firestore keeps a local disk cache by default. Even while the device is offline, snapshots() instantly returns the last known data; when the connection comes back, server-side changes sync automatically. You can tell whether a snapshot came from the local cache or the server with snapshot.metadata.isFromCache — handy for a 'syncing' indicator.
Things to Watch Out For
1. Don't Recreate the Stream Inside build()
If you write the snapshots() call directly inside build(), a NEW stream is created on every rebuild, and StreamBuilder starts listening from scratch — resulting in a brief loading flicker and unnecessary query cost. Create the stream once in initState or as a late final field and keep it:
class _MesajListesiState extends State<MesajListesi> {
late final Stream<QuerySnapshot> _mesajStream = FirebaseFirestore.instance
.collection('mesajlar')
.orderBy('tarih', descending: true)
.snapshots();
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(stream: _mesajStream, builder: (context, snapshot) {
// ...
return const SizedBox.shrink();
});
}
}
2. Large Lists Need Pagination
snapshots() keeps the entire result set live; listening to a collection with thousands of documents without a limit is expensive in both bandwidth and memory. Use startAfterDocument for page-by-page loading, or bound the query to a narrow range (like the last 50 records).
Conclusion
snapshots() and StreamBuilder bring Firestore's most powerful feature — real-time updates — into Flutter with just a few lines of code. Create the stream once and keep it, narrow the query with where/orderBy/limit, and handle ConnectionState and error states completely; done right, the result is an interface that's both fast and efficient.




