Skip to content

Commit fdb1bd8

Browse files
authored
Merge pull request #23 from jlara310/firestore_query_subcollection
docs: add snippet for a subcollection query
2 parents 36812ac + 8a7493a commit fdb1bd8

File tree

2 files changed

+58
-48
lines changed

2 files changed

+58
-48
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

packages/firebase_snippets_app/pubspec.yaml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,35 @@ environment:
2323
dependencies:
2424
flutter:
2525
sdk: flutter
26-
firebase_core: ^1.12.0
27-
cloud_firestore: ^3.1.8
26+
firebase_core: ^2.12.0
27+
cloud_firestore: ^4.7.0
2828
cloud_firestore_odm: ^1.0.0-dev.11
29-
cloud_functions: ^3.2.10
30-
firebase_remote_config: ^2.0.2
31-
firebase_analytics: ^9.1.2
32-
firebase_storage: ^10.2.9
33-
firebase_messaging: ^11.2.11
34-
firebase_dynamic_links: ^4.1.2
35-
firebase_ml_model_downloader: ^0.1.0+9
36-
firebase_auth: ^3.3.12
29+
cloud_functions: ^4.3.0
30+
firebase_remote_config: ^4.2.0
31+
firebase_analytics: ^10.4.0
32+
firebase_storage: ^11.2.0
33+
firebase_messaging: ^14.6.0
34+
firebase_dynamic_links: ^5.3.0
35+
firebase_ml_model_downloader: ^0.2.3
36+
firebase_auth: ^4.6.0
3737
path_provider: ^2.0.9
38-
firebase_database: ^9.0.9
39-
firebase_performance: ^0.8.0+8
38+
firebase_database: ^10.2.0
39+
firebase_performance: ^0.9.2
4040
json_annotation: ^4.4.0
41-
google_sign_in: ^5.2.4
42-
flutter_facebook_auth: ^4.1.2
41+
google_sign_in: ^6.1.0
42+
flutter_facebook_auth: ^5.0.11
4343
github_sign_in: ^0.0.4
4444
twitter_login: ^4.1.0
45-
sign_in_with_apple: ^3.3.0
45+
sign_in_with_apple: ^4.3.0
4646
crypto: ^3.0.1
47-
firebase_crashlytics: ^2.6.2
47+
firebase_crashlytics: ^3.3.0
4848

4949
dev_dependencies:
5050
flutter_test:
5151
sdk: flutter
5252
integration_test:
5353
sdk: flutter
54-
flutter_lints: ^1.0.0
54+
flutter_lints: ^2.0.1
5555
build_runner: ^2.1.8
5656
cloud_firestore_odm_generator: ^1.0.0-dev.11
5757
json_serializable: ^6.1.5

0 commit comments

Comments
 (0)