@@ -3,23 +3,17 @@ import 'package:dart_firebase_admin/dart_firebase_admin.dart';
33import 'package:firebase_functions/firebase_functions.dart' ;
44import 'package:google_cloud_firestore/google_cloud_firestore.dart' ;
55
6- const incrementCallable = 'incrementSynced' ;
7-
86class IncrementResponse {
97 final String message;
108 final int newCount;
119
1210 IncrementResponse ({required this .message, required this .newCount});
1311
14- Map <String , dynamic > toJson () => {
15- 'message' : message,
16- 'newCount' : newCount,
17- };
12+ Map <String , dynamic > toJson () => {'message' : message, 'newCount' : newCount};
1813}
1914
2015void main (List <String > args) async {
2116 await fireUp (args, (firebase) {
22-
2317 // [START dartHttpIncrementLocal]
2418 firebase.https.onRequest (name: 'incrementLocal' , (request) async {
2519 print ('Incrementing counter locally...' );
@@ -53,7 +47,7 @@ void main(List<String> args) async {
5347 // [END dartHttpIncrementLocal]
5448
5549 // [START dartHttpIncrementSynced]
56- firebase.https.onRequest (name: incrementCallable , (request) async {
50+ firebase.https.onRequest (name: 'incrementSynced' , (request) async {
5751 print ('Processing synced counter request...' );
5852
5953 // Get firestore admin instance
@@ -62,11 +56,12 @@ void main(List<String> args) async {
6256 // Get a reference to the counter document
6357 final counterDoc = firestore.collection ('counters' ).doc ('global' );
6458
65- if (request.method == 'GET' ) {
66- // Handle GET request to read the current counter
67- final snapshot = await counterDoc.get ();
68- final currentCount = snapshot.data ()? ['count' ] as int ? ?? 0 ;
59+ // Fetch the current counter value
60+ final snapshot = await counterDoc.get ();
61+ final currentCount = snapshot.data ()? ['count' ] as int ? ?? 0 ;
6962
63+ if (request.method == 'GET' ) {
64+ // Handle GET request to respond with the current counter
7065 final response = IncrementResponse (
7166 message: 'Cloud-sync fetched!' ,
7267 newCount: currentCount,
@@ -79,8 +74,6 @@ void main(List<String> args) async {
7974 );
8075 } else if (request.method == 'POST' ) {
8176 // Handle POST request to increment the counter
82- final snapshot = await counterDoc.get ();
83- final currentCount = snapshot.data ()? ['count' ] as int ? ?? 0 ;
8477
8578 // Increment count by one
8679 await counterDoc.set ({
@@ -102,6 +95,5 @@ void main(List<String> args) async {
10295 }
10396 });
10497 // [END dartHttpIncrementSynced]
105-
10698 });
10799}
0 commit comments