Skip to content

Add avoid_context_read_in_build lint - #547

Open
cupofme wants to merge 3 commits into
masterfrom
feature/avoid_context_read_in_build-lint
Open

Add avoid_context_read_in_build lint#547
cupofme wants to merge 3 commits into
masterfrom
feature/avoid_context_read_in_build-lint

Conversation

@cupofme

@cupofme cupofme commented Jul 16, 2026

Copy link
Copy Markdown

This PR adds a lint for easy to overlook bug: reading a value with context.read inside build method. read takes a one-time snapshot and never re-subscribes, so the UI quietly goes stale — reasonable-looking code that's easy to miss in review and annoying to debug later.

Now the analyzer flags it the moment it's written and offers a one-click fix to swap read for watch. It stays quiet on all the legitimate uses — calling a method, adding an event, grabbing a bloc reference, or reading inside a callback.

// BAD — reads a snapshot, UI won't rebuild when it changes
final count = context.read<CounterCubit>().state;
return Text('$count');

// GOOD — watch keeps the widget in sync
final count = context.watch<CounterCubit>().state;
return Text('$count');

// GOOD — reads that aren't build data stay quiet
onPressed: () => context.read<CounterCubit>().increment(),
read.watch.mov

@cupofme
cupofme requested a review from mchudy as a code owner July 16, 2026 08:38
@github-actions github-actions Bot added the p: leancode_lint Related to the leancode_lint package label Jul 16, 2026
@cupofme cupofme added the enhancement New feature or request label Jul 16, 2026
Comment on lines +118 to +120
final c = context.read<MyCubit>();
c.doThing();
return const SizedBox();'''),

@PiotrRogulski PiotrRogulski Jul 20, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure if this is OK – it's still top-level read in build. I feel like we should flag all reads, no matter what the purpose is 🤔

Invoking a method is still using the value

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Now all build-methods reads are flagged except for ones that are used in callbacks

@PiotrRogulski PiotrRogulski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One last thing 🙏

Comment on lines +18 to +19
/// render leaves the UI stale when the value changes — `watch` (or a
/// `BlocBuilder`/`BlocSelector`) is what's actually wanted.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

select() is also valid (just like BlocSelector). We probably can't suggest it in the fix (because it would require rewriting more code), but we should mention it in docs and messages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request p: leancode_lint Related to the leancode_lint package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants