Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SuperTextField] Fix setState called during build phase (Resolves #1980) #1981

Merged
merged 2 commits into from May 6, 2024

Conversation

angelosilvestre
Copy link
Collaborator

[SuperTextField] Fix setState called during build phase. Resolves #1980

The focus change callback seems to be invoked during the build phase sometimes. In our focus change callback we call setState, which causes a Flutter violation (calling markNeedsBuild during build phase).

I wasn't able to reproduce this issue locally, but the fix should be obvious: when the callback is invoked during build, we need to defer the call to setState until the end of the frame.

@angelosilvestre
Copy link
Collaborator Author

@knopp Could you please try this PR? Or could you provide more info on how to reproduce this issue?

_log.info('Attaching TextInputClient to TextInput');
// The focus change callback might be invoked in the build phase. It that's the case,
// defer the setState call until the end of the frame.
WidgetsBinding.instance.runAsSoonAsPossible(() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we have a method that automatically bails when not mounted?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We have for run code always on the next frame, but not to run it as soon as possible

@matthew-carroll
Copy link
Contributor

@angelosilvestre - Let's also document in code the call path that results in calling the method during build. It sounds like it originates in OverlayPortal.

@knopp
Copy link
Contributor

knopp commented May 1, 2024

@angelosilvestre, this fixed the failing tests that we had. Thanks!

@angelosilvestre
Copy link
Collaborator Author

@angelosilvestre - Let's also document in code the call path that results in calling the method during build. It sounds like it originates in OverlayPortal.

@matthew-carroll Updated.

@knopp
Copy link
Contributor

knopp commented May 3, 2024

@matthew-carroll, can this be merged?

Copy link
Contributor

@matthew-carroll matthew-carroll left a comment

Choose a reason for hiding this comment

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

LGTM

@matthew-carroll matthew-carroll merged commit 0366952 into main May 6, 2024
11 checks passed
@matthew-carroll matthew-carroll deleted the 1980_fix-android-setstate-in-build branch May 6, 2024 21:57
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.

Regression: FlutterError (setState() or markNeedsBuild() called during build.
3 participants