Skip to content

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

Closed
Eimen2018 wants to merge 1 commit into
Stacked-Org:masterfrom
Eimen2018:feat/1118-safe-data-accessor
Closed

Eimen2018 wants to merge 1 commit into
Stacked-Org:masterfrom
Eimen2018: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 Stacked-Org#1118

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

Copy link
Copy Markdown
Collaborator Author

Superseded by #1196, which is the same change opened from a branch in this repository instead of a fork so that CI can post the coverage report.

@Eimen2018 Eimen2018 closed this Sep 18, 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.

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

1 participant