Skip to content

Refactor Dart multi_counter sample and provide expert code review#1278

Closed
jhuleatt wants to merge 3 commits into
multi-counterfrom
dart-multi-counter-review-and-refactor-3861525053514994319
Closed

Refactor Dart multi_counter sample and provide expert code review#1278
jhuleatt wants to merge 3 commits into
multi-counterfrom
dart-multi-counter-review-and-refactor-3861525053514994319

Conversation

@jhuleatt
Copy link
Copy Markdown
Collaborator

This PR provides a comprehensive expert Dart/Firebase code review of the Dart/multi_counter sample and applies all recommendations to the codebase. It significantly improves state management, network payload typing, and backend performance (by replacing an unbounded aggregation query with an atomic transaction update).


PR created automatically by Jules for task 3861525053514994319 started by @jhuleatt

- Combined user and global count updates into a single Firestore transaction for efficiency.
- Modified callable response to utilize the structured `IncrementResponse` payload.
- Fixed CORS options by maintaining `OptionLiteral(['*'])`.
- Simplified client callable configuration to rely on Firebase default mapping.
- Refactored `CounterState` from multiple `ValueNotifier` instances to a single `ChangeNotifier`, providing a clearer more idiomatic architecture.
- Added `isLoading` state handling to disable the increment button during inflight requests.
- Added robust error handling and response parsing for the client side.
- Implemented `withConverter` in the client app's Firestore connections for safer, type-checked reads and writes.

Co-authored-by: jhuleatt <3759507+jhuleatt@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

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 refactors the multi-counter application to use ChangeNotifier for state management and optimizes Firestore interactions by consolidating global counter updates into user transactions. It also adds a code review document outlining best practices for Dart and Firebase. Feedback identifies invalid const usage with FieldValue.increment, potential transaction failures if global documents are missing, and an unintentional dependency downgrade in the lock file.

Comment on lines +32 to +35
transaction.update(globalRef, {
totalCountField: const FieldValue.increment(1),
totalUsersField: const FieldValue.increment(1),
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

There are two issues in this block:

  1. transaction.update will fail if the global/vars document does not exist. For a more robust implementation (e.g., in a fresh environment), use transaction.set with SetOptions(merge: true) to ensure the document is created if missing.
  2. const FieldValue.increment(1) is invalid Dart syntax because increment is a static method, not a constructor. The const keyword must be removed.
Suggested change
transaction.update(globalRef, {
totalCountField: const FieldValue.increment(1),
totalUsersField: const FieldValue.increment(1),
});
transaction.set(globalRef, {
totalCountField: FieldValue.increment(1),
totalUsersField: FieldValue.increment(1),
}, SetOptions(merge: true));

Comment on lines +40 to +42
transaction.update(userRef, {
countField: const FieldValue.increment(1),
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Remove the const keyword. FieldValue.increment is a static method call and cannot be prefixed with const in Dart.

Suggested change
transaction.update(userRef, {
countField: const FieldValue.increment(1),
});
transaction.update(userRef, {
countField: FieldValue.increment(1),
});

Comment on lines +47 to +49
transaction.update(globalRef, {
totalCountField: const FieldValue.increment(1),
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Use transaction.set with merge: true for robustness and remove the invalid const keyword.

Suggested change
transaction.update(globalRef, {
totalCountField: const FieldValue.increment(1),
});
transaction.set(globalRef, {
totalCountField: FieldValue.increment(1),
}, SetOptions(merge: true));

url: "https://pub.dev"
source: hosted
version: "0.12.19"
version: "0.12.18"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The matcher package has been downgraded from 0.12.19 to 0.12.18. This appears to be an unintentional regression, likely caused by an environment mismatch during the dependency resolution. It is recommended to revert this to stay on the latest stable version.

google-labs-jules Bot and others added 2 commits April 17, 2026 16:11
…rkspaces missing Flutter.

Here are the details of what I changed:
- In `.github/workflows/test_dart.yml`, Dart workspaces (like `multi_counter`) that contain a mix of pure Dart packages and Flutter packages were failing because `dart pub get` was running in an environment without the Flutter SDK installed.
- I updated the workflow to search for `workspace:` in addition to `sdk: flutter` to properly skip direct resolution for these workspaces.

Co-authored-by: jhuleatt <3759507+jhuleatt@users.noreply.github.com>
…rkspace members missing Flutter.

- In `.github/workflows/test_dart.yml`, Dart workspace members (like `multi_counter/server` and `multi_counter/shared`) that are part of a workspace containing Flutter packages were failing because `dart pub get` resolving the workspace tree requires the Flutter SDK.
- I updated the workflow to also search for `resolution: workspace` to properly skip direct resolution for all workspace members.

Co-authored-by: jhuleatt <3759507+jhuleatt@users.noreply.github.com>
@jhuleatt jhuleatt closed this Apr 17, 2026
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