Search⌘ K
AI Features

Examples of Queries

Understand how to write and execute collection queries in Firestore within a Flutter app. Learn to fetch document snapshots using get() for one-time reads and snapshot() for real-time streaming updates, essential for integrating Firebase with your mobile app.

We'll cover the following...

After setting up the Firestore database, let’s see how to write our first collection query to it.

Collection query

Dart
import 'package:cloud_firestore/cloud_firestore.dart';
FirebaseFirestore firestore = FirebaseFirestore.instance;
// Setting up the firestore collection called 'users'.
CollectionReference users = FirebaseFirestore.instance.collection('users');
Future<void> addUser() {
// Adding a new user
return users
.add({
'name': 'Michael',
'email': 'michael.c@gmail.com',
'age': 42
})
.then((value) => print("User Added"))
.catchError((error) => print("Error occurred: $error"));
}

In Firestore, we have, at the root level, a collection. (Let’s say users.) This collection now ...