What is the difference between set() and add()?
add() creates a doc with a random auto-generated ID. set() uses the ID you specify with doc().
Store and sync app data in real-time with Firestore
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
Cloud Firestore is a flexible and scalable NoSQL cloud database provided by Firebase. It stores data in documents organized into collections, similar to JSON objects. One of its most powerful features is real-time synchronization, meaning any change on the server is instantly pushed to all connected clients. Firestore also has built-in offline support so your app continues to work even without an internet connection and syncs changes automatically when connectivity returns. You can query, filter, order, and paginate data using a chainable query API.
1// pubspec.yaml: cloud_firestore: ^4.13.02import 'package:cloud_firestore/cloud_firestore.dart';34final db = FirebaseFirestore.instance;56// ── Create / Set Document ──7await db.collection('users').doc('user123').set({8'name': 'Ahmed',9'email': 'ahmed@email.com',10'createdAt': FieldValue.serverTimestamp(),11});1213// ── Add (auto-generated ID) ──14final ref = await db.collection('posts').add({15'title': 'My First Post',16'content': 'Hello World!',17'likes': 0,18});19print('New doc ID: ${ref.id}');2021// ── Read One Document ──22final doc = await db.collection('users').doc('user123').get();23if (doc.exists) {24print(doc.data()); // Map<String, dynamic>25}2627// ── Read All Documents ──28final snapshot = await db.collection('posts')29.where('likes', isGreaterThan: 5)30.orderBy('likes', descending: true)31.limit(10)32.get();3334for (final doc in snapshot.docs) {35print('${doc.id}: ${doc.data()}');36}3738// ── Real-time Listener ──39db.collection('posts').snapshots().listen((snapshot) {40for (final change in snapshot.docChanges) {41if (change.type == DocumentChangeType.added) {42print('New: ${change.doc.data()}');43}44}45});4647// ── Update ──48await db.collection('posts').doc(ref.id).update({49'likes': FieldValue.increment(1),50});5152// ── Delete ──53await db.collection('posts').doc(ref.id).delete();
Use get() for one-time reads like loading a user profile or fetching a settings document. Use snapshots() for real-time updates where data can change frequently, like a chat app, live feed, or collaborative editor. Real-time listeners keep the UI in sync automatically without manual refresh.
Model collections around read patterns, not only around object names. Ask first: what screens must load quickly?
When data model follows query paths, performance and costs stay predictable at scale.
Store and sync app data in real time with Firestore. Backend integration connects app actions to real user data and secure business workflows.
Think in architecture, reliability, and team-level maintainability.
Define data model and access rules before writing convenience helper methods.
Security and data integrity issues can damage user trust faster than UI bugs.
Security rules are part of architecture, not optional deployment polish.
As your app grows, keep UI, state, and data responsibilities separate so each part stays testable and easier to modify.
add() creates a doc with a random auto-generated ID. set() uses the ID you specify with doc().
snapshots() returns a Stream that emits whenever the collection data changes.
FieldValue.increment(n) atomically increments a numeric field without reading the current value first.
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