Skip to content

Commit b116379

Browse files
committed
docs: add snippet for a subcollection query
1 parent 36812ac commit b116379

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

packages/firebase_snippets_app/lib/snippets/firestore.dart

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class FirestoreSnippets extends DocSnippet {
3434
getStarted_readData();
3535
dataModel_references();
3636
dataModel_subCollections();
37+
getDataOnce_getAllDocumentsInASubcollection();
3738
}
3839

3940
void getStarted_addData() async {
@@ -453,31 +454,47 @@ class FirestoreSnippets extends DocSnippet {
453454
void getDataOnce_multipleDocumentsFromACollection() {
454455
// [START get_data_once_multiple_documents_from_a_collection]
455456
db.collection("cities").where("capital", isEqualTo: true).get().then(
456-
(querySnapshot) {
457-
print("Successfully completed");
458-
for (var docSnapshot in querySnapshot.docs) {
459-
print('${docSnapshot.id} => ${docSnapshot.data()}');
460-
}
461-
},
462-
onError: (e) => print("Error completing: $e"),
463-
);
457+
(querySnapshot) {
458+
print("Successfully completed");
459+
for (var docSnapshot in querySnapshot.docs) {
460+
print('${docSnapshot.id} => ${docSnapshot.data()}');
461+
}
462+
},
463+
onError: (e) => print("Error completing: $e"),
464+
);
464465
// [END get_data_once_multiple_documents_from_a_collection]
465466
}
466467

467468
void getDataOnce_getAllDocumentsInACollection() {
468469
// [START get_data_once_get_all_documents_in_a_collection]
469470
db.collection("cities").get().then(
470-
(querySnapshot) {
471-
print("Successfully completed");
472-
for (var docSnapshot in querySnapshot.docs) {
473-
print('${docSnapshot.id} => ${docSnapshot.data()}');
474-
}
475-
},
476-
onError: (e) => print("Error completing: $e"),
477-
);
471+
(querySnapshot) {
472+
print("Successfully completed");
473+
for (var docSnapshot in querySnapshot.docs) {
474+
print('${docSnapshot.id} => ${docSnapshot.data()}');
475+
}
476+
},
477+
onError: (e) => print("Error completing: $e"),
478+
);
478479
// [END get_data_once_get_all_documents_in_a_collection]
479480
}
480481

482+
void getDataOnce_getAllDocumentsInASubcollection() {
483+
// [START get_data_once_get_all_documents_in_a_subcollection]
484+
// [START firestore_query_subcollection]
485+
db.collection("cities").doc("SF").collection("landmarks").get().then(
486+
(querySnapshot) {
487+
print("Successfully completed");
488+
for (var docSnapshot in querySnapshot.docs) {
489+
print('${docSnapshot.id} => ${docSnapshot.data()}');
490+
}
491+
},
492+
onError: (e) => print("Error completing: $e"),
493+
);
494+
// [END firestore_query_subcollection]
495+
// [END get_data_once_get_all_documents_in_a_subcollection]
496+
}
497+
481498
void getDataOnce_listSubCollections() {
482499
// [START get_data_once_list_sub_collections]
483500
// Not currently available in Dart SDK
@@ -807,27 +824,20 @@ class FirestoreSnippets extends DocSnippet {
807824
void aggregationQuery_count() {
808825
// [START count_aggregate_collection]
809826
// Returns number of documents in users collection
810-
db
811-
.collection("users")
812-
.count()
813-
.then(
814-
(res) => print(res.data().count),
815-
onError: (e) => print("Error completing: $e"),
816-
);
827+
db.collection("users").count().then(
828+
(res) => print(res.data().count),
829+
onError: (e) => print("Error completing: $e"),
830+
);
817831
// [END count_aggregate_collection]
818832
}
819833

820834
void aggregationQuery_count2() {
821835
// [START count_aggregate_query]
822836
// This also works with collectionGroup queries.
823-
db
824-
.collection("users")
825-
.where("age", isGreaterThan: 10)
826-
.count()
827-
.then(
828-
(res) => print(res.data().count),
829-
onError: (e) => print("Error completing: $e"),
830-
);
837+
db.collection("users").where("age", isGreaterThan: 10).count().then(
838+
(res) => print(res.data().count),
839+
onError: (e) => print("Error completing: $e"),
840+
);
831841
// [END count_aggregate_query]
832842
}
833843

0 commit comments

Comments
 (0)