CodeBlanks Logo Icon CodeBlanks

Read data

Read in realtime with collection().snapshots.

The snapshots method

The collection().snapshots() method returns a stream that watches the documents in a collection in real time. When data is added, changed, or removed, the stream emits a new QuerySnapshot. This lets the UI update automatically without re-fetching the data.

db.collection('messages').snapshots().listen((snap) {
  for (final doc in snap.docs) {
    print(doc.data());
  }
});

QuerySnapshot and docs

QuerySnapshot holds all documents in the query result. You access the list of documents with snap.docs; each document yields its fields as a Map via doc.data(). doc.id is the document id.

snap.docs.forEach((doc) {
  print(doc.id);
  print(doc.data());
});

Practice Listen to documents and count them

Get db. Listen to the snapshots stream of the 'messages' collection. Print the document count on each snapshot. Wait 10 milliseconds after starting to listen.

Starter code

main.dart
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
}

Sample solution

main.dart
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));
}

Expected output

1

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does the snapshots method return?

  • Stream<QuerySnapshot> Correct answer
  • Future<QuerySnapshot>
  • Future<List>
  • DocumentReference

Explanation: snapshots returns a real-time Stream<QuerySnapshot>; a new snapshot is emitted on every data change.

2 Listen to the documents

Question
db.collection('messages').____().listen((snap) {
  print(snap.docs.length);
});
Answer
db.collection('messages').snapshots().listen((snap) {
  print(snap.docs.length);
});

Explanation: To watch collection documents in real time you use the snapshots method; it returns a stream.

3 Reading parts

Term Meaning
snapshots Real-time stream
QuerySnapshot Query result
docs Document list
doc.data() Document fields

All lessons in Advanced