Skip to content

Increment example#1259

Open
jhuleatt wants to merge 5 commits intodart-launchfrom
codelab-dart
Open

Increment example#1259
jhuleatt wants to merge 5 commits intodart-launchfrom
codelab-dart

Conversation

@jhuleatt
Copy link
Copy Markdown
Collaborator

@jhuleatt jhuleatt commented Apr 8, 2026

No description provided.

@jhuleatt jhuleatt changed the base branch from main to dart-launch April 8, 2026 14:35
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces several Dart quickstart samples for Firebase Cloud Functions, including examples for callable functions with streaming, basic HTTPS triggers, and Firestore integration. The review feedback identifies a race condition in the Firestore increment logic that should be resolved using a transaction, suggests improving error handling in the weather and time-server APIs to ensure proper error propagation to clients, and recommends tightening Firestore security rules to follow the principle of least privilege.

I am having trouble creating individual review comments. Click here to see my feedback.

Dart/quickstarts/https-increment-number/bin/server.dart (82-93)

high

This implementation contains a race condition. The currentCount is fetched before the increment operation. In a concurrent environment, multiple requests might read the same initial value, resulting in the function returning the same newCount even though the database was incremented multiple times. To ensure consistency and return the correct updated value, use a transaction.

        final newCount = await firestore.runTransaction((transaction) async {
          final snapshot = await transaction.get(counterDoc);
          final currentCount = snapshot.data()?['count'] as int? ?? 0;
          final nextCount = currentCount + 1;
          transaction.set(counterDoc, {'count': nextCount}, options: SetOptions.merge());
          return nextCount;
        });

        final response = IncrementResponse(
          message: 'Cloud-sync complete!',
          newCount: newCount,
        );

Dart/quickstarts/callable-functions-streaming/bin/server.dart (31)

medium

Returning a string error message results in a successful response from the function's perspective. To properly signal an error to the client and the Firebase runtime, you should throw an exception. This ensures that the error is correctly propagated and can be handled by the client SDK's error handling mechanisms.

    throw Exception('Weather API error: ${resp.statusCode}');

Dart/quickstarts/callable-functions-streaming/bin/server.dart (39)

medium

Similar to the previous check, returning a string error message here will be treated as a success. Throw an exception instead to ensure the failure is properly reported.

    throw Exception('Forecast API error: ${forecastResp.statusCode}');

Dart/quickstarts/https-increment-number/firestore.rules (4-6)

security-medium medium

Allowing public read access to the entire database is insecure. It is a security best practice to restrict access to only the specific document or collection used by this quickstart.

    match /counters/global {
      allow read: if true;
    }

Dart/quickstarts/https-time-server/bin/server.dart (77-79)

medium

The DateFormat constructor throws an ArgumentError if the provided format string is invalid. This would cause the function to crash with a 500 Internal Server Error. It is better to catch this error and return a 400 Bad Request to the client.

      try {
        final formattedDate = DateFormat(format).format(DateTime.now());
        print('Sending formatted date: $formattedDate');
        return Response.ok(formattedDate);
      } catch (e) {
        return Response.badRequest(body: 'Invalid date format');
      }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant