Add avoid_context_read_in_build lint - #547
Open
cupofme wants to merge 3 commits into
Open
Conversation
Comment on lines
+118
to
+120
| final c = context.read<MyCubit>(); | ||
| c.doThing(); | ||
| return const SizedBox();'''), |
Member
There was a problem hiding this comment.
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
Author
There was a problem hiding this comment.
Now all build-methods reads are flagged except for ones that are used in callbacks
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. |
Member
There was a problem hiding this comment.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds a lint for easy to overlook bug: reading a value with
context.readinsidebuildmethod.readtakes 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
readforwatch. It stays quiet on all the legitimate uses — calling a method, adding an event, grabbing a bloc reference, or reading inside a callback.read.watch.mov