Skip to content

feat(state): add context.select for granular page-scoped reads#1058

Merged
jacobaraujo7 merged 2 commits into
masterfrom
feat/context-select
Jun 23, 2026
Merged

feat(state): add context.select for granular page-scoped reads#1058
jacobaraujo7 merged 2 commits into
masterfrom
feat/context-select

Conversation

@jacobaraujo7

Copy link
Copy Markdown
Contributor

What

Adds context.select<T, R>(selector) to ModularStateX — the method-based twin of the existing Selector widget. It mirrors context.select from provider to make migration a near drop-in.

// Inside build(): rebuilds only when cart.items.length changes.
final count = context.select<CartViewModel, int>((cart) => cart.items.length);

The calling widget rebuilds only when the selected value changes (==), instead of on every notification (which is what context.watch does).

Why

We already ship Consumer/Selector (provider-style widgets). context.select rounds out the migration surface so projects coming from provider can move method-by-method without restructuring their widget trees.

How

A plain InheritedNotifier notifies every dependent on each trigger, so it can't do per-widget selection on its own. _VMInherited now uses a custom InheritedElement that:

  • reproduces InheritedNotifier's trigger-driven rebuild behavior, and
  • honors per-dependent selector aspects — on each notification it re-evaluates each selecting dependent's selector and only rebuilds it when the derived value changed.

A watch dependent (no aspect) is still notified on every trigger, so watch/read/Consumer/Selector behavior is unchanged.

Tests

  • New test/select_test.dart: rebuilds only on selected change, ignores unrelated changes, keeps tracking across rebuilds, and throws when no scoped T is provided.
  • Full suite green (56 tests), flutter analyze clean.

Notes

  • Bumps flutter_modular to 7.0.2 (README + CHANGELOG + v7 state-management docs updated).
  • Docs MCP re-embedded and bumped to 0.2.2 (tool/docs_mcp), binary recompiled & smoke-tested locally.

🤖 Generated with Claude Code

jacobaraujo7 and others added 2 commits June 23, 2026 11:09
Adds `context.select<T, R>(selector)` to `ModularStateX` — the method-based
twin of the existing `Selector` widget, mirroring `context.select` from
`provider` to ease migration. The calling widget rebuilds only when the
selected value changes (`==`), instead of on every notification.

To support per-dependent selection over a page-scoped value, `_VMInherited`
now uses a custom `InheritedElement` that reproduces `InheritedNotifier`'s
trigger-driven rebuilds while honoring per-dependent selector aspects (a
`watch` dependent — no aspect — is still notified on every trigger).

Bumps flutter_modular to 7.0.2 and documents the API in the README and the
v7 state-management page.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Re-embeds the state-management page documenting `context.select<T, R>` and
bumps the server version to 0.2.2 (pubspec + serverVersion handshake).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jacobaraujo7 jacobaraujo7 merged commit 9a3cc63 into master Jun 23, 2026
2 of 3 checks passed
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