Skip to content

feat(viewmodels): add safeData accessor to DataStateHelper - #1196

Open
Eimen2018 wants to merge 2 commits into
masterfrom
feat/1118-safe-data-accessor
Open

Eimen2018 wants to merge 2 commits into
masterfrom
feat/1118-safe-data-accessor

Conversation

@Eimen2018

Copy link
Copy Markdown
Collaborator

Summary

Adds a safeData getter to DataStateHelper<T> (used by FutureViewModel and StreamViewModel).

data is T? because it starts unset and is cleared on error, so Dart cannot promote it to non-null even right after a dataReady check. Callers end up writing data! everywhere. safeData performs the dataReady check itself and returns a non-null T, or throws a descriptive StateError pointing the caller at dataReady / isBusy / hasError.

if (viewModel.dataReady) {
  final value = viewModel.safeData; // no `!` needed
}

This is the accessor agreed on in the issue thread; the requested PR was never submitted.

Changes

  • lib/src/view_models/helpers/data_state_helper.dart: new T get safeData with dartdoc. Existing data and dataReady are untouched.
  • test/data_state_helper_test.dart: 8 tests covering ready, never set, busy, error, error message content, and nullable T behaviour.

Notes

  • Purely additive, no signature changes, no new dependencies.
  • MultipleFutureViewModel / MultipleStreamViewModel were left alone: their dataMap is Map<K, dynamic>? on a private base class, so a keyed safeData would give no type-safety benefit.
  • Nullable T whose legitimate value is null is never dataReady today; safeData keeps that semantic and documents it.

Verification

  • flutter test: 101 passed
  • dart analyze lib test: no new issues

Closes #1118

🤖 Generated with Claude Code

Returns the non-null data when dataReady is true and throws a descriptive StateError otherwise, so callers no longer need the null assertion operator after checking dataReady.

Closes #1118

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

LCOV of commit f3bfa47 during Test Package #151

📊 Summary Coverage

Metric Coverage Details
📈 Lines 🔴 7.7% 283 / 3692 lines
🔧 Functions ⚪ N/A no data found
🌿 Branches ⚪ N/A no data found

📁 Files Changed Coverage

File Lines Functions Branches
lib/src/code_generation/router_annotation/extended_navigator.dart ⚪ – (0) ⚪ – (0) ⚪ – (0)
lib/src/view_models/helpers/data_state_helper.dart 🔴 0.0% (8) ⚪ – (0) ⚪ – (0)

🟢 ≥ 90% · 🟡 ≥ 70% · 🔴 below 70% · ⚪ no data
Full coverage report

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

@ferrarafer ferrarafer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, thanks! The getter only returns when dataReady is true, so the cast is safe, and existing callers aren't affected.

One thing to be aware of: safeData throws while the view model is busy, even when valid data is already loaded (for example when a FutureViewModel re-runs initialise(), or a StreamViewModel runs another action through runBusyFuture). So views that keep showing the previous data during a refresh should keep using data. This matches how dataReady already behaves, so no change is needed.

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.

[feature]: Asserting values are present when viewmodel.dataReady is true

2 participants