Skip to content

Commit 9876574

Browse files
committed
feat(firestore): add samples for sum and average
1 parent 2d9d956 commit 9876574

File tree

1 file changed

+55
-15
lines changed

1 file changed

+55
-15
lines changed

packages/firebase_snippets_app/lib/snippets/firestore.dart

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -823,25 +823,18 @@ class FirestoreSnippets extends DocSnippet {
823823

824824
void filterQuery_or() {
825825
// [START firestore_query_filter_or]
826-
var query = db.collection("cities")
827-
.where(
828-
Filter.or(
829-
Filter("capital", isEqualTo: true),
830-
Filter("population", isGreaterThan: 1000000)
831-
));
826+
var query = db.collection("cities").where(Filter.or(
827+
Filter("capital", isEqualTo: true),
828+
Filter("population", isGreaterThan: 1000000)));
832829
// [END firestore_query_filter_or]
833830
}
834831

835832
void filterQuery_or2() {
836833
// [START firestore_query_filter_or_compound]
837-
var query = db.collection("cities")
838-
.where(
839-
Filter.and(
840-
Filter("state", isEqualTo: "CA"),
841-
Filter.or(
842-
Filter("capital", isEqualTo: true),
843-
Filter("population", isGreaterThan: 1000000)
844-
)));
834+
var query = db.collection("cities").where(Filter.and(
835+
Filter("state", isEqualTo: "CA"),
836+
Filter.or(Filter("capital", isEqualTo: true),
837+
Filter("population", isGreaterThan: 1000000))));
845838
// [END firestore_query_filter_or_compound]
846839
}
847840

@@ -865,6 +858,52 @@ class FirestoreSnippets extends DocSnippet {
865858
// [END count_aggregate_query]
866859
}
867860

861+
void aggregationQuery_sum() {
862+
// [START sum_aggregate_collection]
863+
db.collection("users").aggregate(sum("age")).get().then(
864+
(res) => print(res.getAverage("age")),
865+
onError: (e) => print("Error completing: $e"),
866+
);
867+
// [END sum_aggregate_collection]
868+
}
869+
870+
void aggregationQuery_sum2() {
871+
// [START sum_aggregate_query]
872+
db
873+
.collection("users")
874+
.where("age", isGreaterThan: 10)
875+
.aggregate(sum("age"))
876+
.get()
877+
.then(
878+
(res) => print(res.getAverage("age")),
879+
onError: (e) => print("Error completing: $e"),
880+
);
881+
// [END sum_aggregate_query]
882+
}
883+
884+
void aggregationQuery_average() {
885+
// [START average_aggregate_collection]
886+
db.collection("users").aggregate(average("age")).get().then(
887+
(res) => print(res.getAverage("age")),
888+
onError: (e) => print("Error completing: $e"),
889+
);
890+
// [END average_aggregate_collection]
891+
}
892+
893+
void aggregationQuery_average2() {
894+
// [START average_aggregate_query]
895+
db
896+
.collection("users")
897+
.where("age", isGreaterThan: 10)
898+
.aggregate(average("age"))
899+
.get()
900+
.then(
901+
(res) => print(res.getAverage("age")),
902+
onError: (e) => print("Error completing: $e"),
903+
);
904+
// [END average_aggregate_query]
905+
}
906+
868907
void orderAndLimitData_orderAndLimitData() {
869908
// [START order_and_limit_data_order_and_limit_data]
870909
final citiesRef = db.collection("cities");
@@ -951,7 +990,8 @@ class FirestoreSnippets extends DocSnippet {
951990
final next = db
952991
.collection("cities")
953992
.orderBy("population")
954-
.startAfterDocument(lastVisible).limit(25);
993+
.startAfterDocument(lastVisible)
994+
.limit(25);
955995

956996
// Use the query for pagination
957997
// ...

0 commit comments

Comments
 (0)