Skip to content

fix: prevent LateInitializationError when accessing untracked val#171

Merged
nank1ro merged 9 commits into
mainfrom
fix/computed-untrack
Apr 13, 2026
Merged

fix: prevent LateInitializationError when accessing untracked val#171
nank1ro merged 9 commits into
mainfrom
fix/computed-untrack

Conversation

@nank1ro
Copy link
Copy Markdown
Owner

@nank1ro nank1ro commented Apr 13, 2026

closes #170

Accessing untrackedValue before that threw a LateInitializationError because the late field was never populated.

  • hasValue now triggers computation if not yet initialized
  • untrackedValue adds an assert with a helpful message
  • Add _initialized flag to track whether the selector has run

Summary by CodeRabbit

  • Bug Fixes

    • Fixed LateInitializationError when accessing computed values prematurely. Lazy computation now properly triggers on property access, and attempting incorrect access patterns now returns clearer error messages.
  • Tests

    • Added tests validating lazy initialization behavior for computed values.

nank1ro added 3 commits April 13, 2026 15:42
…dValue

Computed values are lazy — the selector only runs on first .value access.
Accessing untrackedValue before that threw a LateInitializationError because
the late field was never populated.

- hasValue now triggers computation if not yet initialized
- untrackedValue adds an assert with a helpful message
- Add _initialized flag to track whether the selector has run
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 13, 2026

Warning

Rate limit exceeded

@nank1ro has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 17 minutes and 3 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 17 minutes and 3 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f3da91be-2084-4a2a-81fe-0d14d3fd34e9

📥 Commits

Reviewing files that changed from the base of the PR and between 3b353fb and aa536a7.

📒 Files selected for processing (4)
  • examples/todos/lib/widgets/todos_list.dart
  • examples/todos/test/widget_test.dart
  • packages/flutter_solidart/test/flutter_solidart_test.dart
  • packages/solidart/lib/src/core/computed.dart

Walkthrough

This PR implements initialization tracking for the Computed class to prevent LateInitializationError when accessing untrackedValue. It adds an _initialized flag, makes hasValue trigger lazy computation, enforces initialization checks on untrackedValue, updates the version to 2.8.4, and includes corresponding unit tests.

Changes

Cohort / File(s) Summary
Version & Documentation
packages/solidart/CHANGELOG.md, packages/solidart/pubspec.yaml
Version bump to 2.8.4 with changelog entry documenting the fix for LateInitializationError prevention via initialization tracking.
Core Implementation
packages/solidart/lib/src/core/computed.dart
Added _initialized flag to track computation state. hasValue now triggers lazy computation when not initialized. untrackedValue asserts initialization with instructive error message.
Tests
packages/solidart/test/solidart_test.dart
Added two test cases validating lazy initialization behavior: confirming hasValue triggers computation and untrackedValue requires prior initialization access.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related issues

Poem

🐰 Hop along to version 2.8.4,
No more lazy errors at the door,
Computed values now track their birth,
With hasValue proving their worth,
A clearer path from start to end,
On initialization we depend! 🎉

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: preventing LateInitializationError when accessing untrackedValue. It directly reflects the primary fix implemented across the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/computed-untrack

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (40119f4) to head (aa536a7).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##              main      #171   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           18        18           
  Lines          931       935    +4     
=========================================
+ Hits           931       935    +4     
Files with missing lines Coverage Δ
packages/solidart/lib/src/core/computed.dart 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/solidart/lib/src/core/computed.dart (1)

126-128: Comment is misleading about initialization state.

Line 126 says computed is “always initialized”, but Line 111 starts _initialized as false. Please reword to avoid confusion for future maintenance.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/solidart/lib/src/core/computed.dart` around lines 126 - 128, The
comment above the Computed implementation is misleading: it states a computed
signal is "always initialized" while the private field `_initialized` is
initialized to `false`; update the comment near the `Computed` class / the
`_initialized` field (and the `hasValue` behavior comment) to accurately reflect
that the computed's value is lazy and `_initialized` starts as false until the
first access triggers evaluation, e.g., change "always initialized" to
"structurally initialized but value is lazy" or similar concise wording so
`_initialized`'s semantics match the comment.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/solidart/lib/src/core/computed.dart`:
- Around line 187-192: The assert-only guard using _initialized must be replaced
with a runtime check that throws a clear exception when the computed hasn't been
initialized to avoid release-mode crashes: in the Computed class replace the
assert on _initialized (that returns _untrackedValue) with an if-check that
throws a StateError (or LateInitializationError with a helpful message) when
!_initialized, and likewise add a runtime guard in the value getter (and any
direct accesses to _untrackedValue, e.g., hasValue logic) to throw if the
computed is disposed or uninitialized instead of directly returning
_untrackedValue; reference symbols: _initialized, _untrackedValue, value getter,
and any disposed flag used by Computed.

---

Nitpick comments:
In `@packages/solidart/lib/src/core/computed.dart`:
- Around line 126-128: The comment above the Computed implementation is
misleading: it states a computed signal is "always initialized" while the
private field `_initialized` is initialized to `false`; update the comment near
the `Computed` class / the `_initialized` field (and the `hasValue` behavior
comment) to accurately reflect that the computed's value is lazy and
`_initialized` starts as false until the first access triggers evaluation, e.g.,
change "always initialized" to "structurally initialized but value is lazy" or
similar concise wording so `_initialized`'s semantics match the comment.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3281cfc7-46ff-4e8a-af2f-bb5d322ee82f

📥 Commits

Reviewing files that changed from the base of the PR and between 40119f4 and 3b353fb.

📒 Files selected for processing (4)
  • packages/solidart/CHANGELOG.md
  • packages/solidart/lib/src/core/computed.dart
  • packages/solidart/pubspec.yaml
  • packages/solidart/test/solidart_test.dart

Comment thread packages/solidart/lib/src/core/computed.dart
@nank1ro nank1ro merged commit 20c2a03 into main Apr 13, 2026
4 checks passed
@nank1ro nank1ro deleted the fix/computed-untrack branch April 13, 2026 14:50
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.

[bug] Computed.untrackedValue trigger throws LateInitializationError

1 participant