Veri okuma
- Flutter kursu
- İleri seviye
- Ders 23/69
- 4 dk
collection().snapshots ile gerçek zamanlı okumayı öğrenirsin.
snapshots metodu
collection().snapshots() metodu, koleksiyondaki belgeleri gerçek zamanlı izleyen bir akış döndürür. Veri eklendiğinde, değiştiğinde veya silindiğinde akış yeni bir QuerySnapshot yayar. Bu sayede veriyi yeniden çekmeden arayüz otomatik güncellenir.
db.collection('messages').snapshots().listen((snap) {
for (final doc in snap.docs) {
print(doc.data());
}
});QuerySnapshot ve docs
QuerySnapshot, sorgu sonucundaki tüm belgeleri tutar. snap.docs ile belge listesine erişirsin; her belge doc.data() ile alanları bir Map olarak verir. doc.id ise belge kimliğidir.
snap.docs.forEach((doc) {
print(doc.id);
print(doc.data());
});Uygulama Belgeleri dinle ve say
Db al. 'messages' koleksiyonunun snapshots akışını dinle. Her snapshot'ta belge sayısını yazdır. Dinlemeye başladıktan sonra 10 milisaniye bekle.
Başlangıç kodu
class QueryDocumentSnapshot {
final Map<String, dynamic> data;
QueryDocumentSnapshot(this.data);
}
class QuerySnapshot {
final List<QueryDocumentSnapshot> docs;
QuerySnapshot(this.docs);
}
class CollectionReference {
final String name;
CollectionReference(this.name);
Stream<QuerySnapshot> snapshots() async* {
yield QuerySnapshot([
QueryDocumentSnapshot({'text': 'Merhaba'}),
]);
}
}
class FirebaseFirestore {
static final FirebaseFirestore instance = FirebaseFirestore._();
FirebaseFirestore._();
CollectionReference collection(String name) => CollectionReference(name);
}
void main() async {
// snapshots akışını dinle
// her snapshot'ta belge sayısını yazdır
// 10ms bekle
}Örnek çözüm
class QueryDocumentSnapshot {
final Map<String, dynamic> data;
QueryDocumentSnapshot(this.data);
}
class QuerySnapshot {
final List<QueryDocumentSnapshot> docs;
QuerySnapshot(this.docs);
}
class CollectionReference {
final String name;
CollectionReference(this.name);
Stream<QuerySnapshot> snapshots() async* {
yield QuerySnapshot([
QueryDocumentSnapshot({'text': 'Merhaba'}),
]);
}
}
class FirebaseFirestore {
static final FirebaseFirestore instance = FirebaseFirestore._();
FirebaseFirestore._();
CollectionReference collection(String name) => CollectionReference(name);
}
void main() async {
final db = FirebaseFirestore.instance;
db.collection('messages').snapshots().listen((snap) {
print(snap.docs.length);
});
await Future.delayed(Duration(milliseconds: 10));
}Beklenen çıktı
1
Soru ve cevaplar
Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.
1 snapshots metodu ne döndürür?
- Stream<QuerySnapshot> Doğru cevap
- Future<QuerySnapshot>
- Future<List>
- DocumentReference
Açıklama: snapshots, gerçek zamanlı bir Stream<QuerySnapshot> döndürür; her veri değişiminde yeni snapshot yayılır.
2 Belgeleri dinle
db.collection('messages').____().listen((snap) {
print(snap.docs.length);
});
db.collection('messages').snapshots().listen((snap) {
print(snap.docs.length);
});
Açıklama: Koleksiyondaki belgeleri gerçek zamanlı izlemek için snapshots metodu kullanılır; akış döndürür.
3 Okuma parçaları
| Kavram | Anlamı |
|---|---|
snapshots |
Gerçek zamanlı akış |
QuerySnapshot |
Sorgu sonucu |
docs |
Belge listesi |
doc.data() |
Belge alanları |