Skip to content

[material_ui] Fix Switch throwing when first painted mid-animation - #13496

Merged
auto-submit[bot] merged 1 commit into
flutter:mainfrom
muralikrishmt:fix-switch-first-paint-179532
Nov 5, 2026
Merged

auto-submit[bot] merged 1 commit into
flutter:mainfrom
muralikrishmt:fix-switch-first-paint-179532

Conversation

@muralikrishmt

@muralikrishmt muralikrishmt commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

_SwitchPainter only initializes its pressed-state fields inside paint(). If the painter's first paint happens in an unusual animation state, those fields are read before they are ever written:

First paint happens… Switch / Switch.adaptive on Android, Fuchsia, Linux, Windows Switch.adaptive on iOS, macOS
while the press reaction is reversing LateInitializationError (_pressedThumbExtension) Null check operator used on a null value (_pressedInactiveThumbRadius!)
during the toggle animation OK Null check operator used on a null value (_pressedInactiveThumbRadius!)

This can happen, for example, when a Switch.adaptive in a ListView's cache extent (laid out but not painted) changes value and is scrolled into view during its toggle animation, or when a switch under Opacity(0) is long-pressed and becomes visible in onChanged.

This PR defaults _pressedThumbExtension to 0 and falls back to the unpressed thumb radii in the Cupertino branch, as the Material branch already does. Initializing _pressedThumbExtension alone (as suggested in the issue) is not enough, because the iOS/macOS null check error comes from _pressedInactiveThumbRadius!.

Two regression tests cover both triggers on all platforms (TargetPlatformVariant.all()). Without the fix, 8 of the 12 variants fail.

Fixes flutter/flutter#179532

%23%23 Pre-Review Checklist

If you need help, consider asking for advice on the %23hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2

_SwitchPainter only initializes its pressed-state fields inside paint().
If the painter's first paint happens while the press reaction is
reversing, or (for the Cupertino adaptive look) in the middle of the
toggle animation, those fields are read before they are written. This
throws a LateInitializationError for `_pressedThumbExtension` on the
Material path, and a null check error for `_pressedInactiveThumbRadius!`
on iOS and macOS.

Default `_pressedThumbExtension` to 0 and fall back to the unpressed
thumb radii in the Cupertino branch, as the Material branch already does.

Fixes flutter/flutter#179532
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Oct 30, 2026
@github-actions github-actions Bot added p: material_ui triage-design Should be looked at in design triage labels Oct 30, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request resolves an issue where Switch and Switch.adaptive could throw a LateInitializationError or a null check error when first painted during a press or toggle animation. The fix initializes _pressedThumbExtension to 0 and introduces local variables effectivePressedInactiveThumbRadius and effectivePressedActiveThumbRadius to safely handle null values, removing force-unwraps. Additionally, corresponding regression tests and a changelog entry have been added. There are no review comments, and no further feedback is provided.

@Piinks Piinks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Thank you!

@Piinks Piinks added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 5, 2026
@auto-submit
auto-submit Bot merged commit 22027c5 into flutter:main Nov 5, 2026
12 checks passed
flutter-zl pushed a commit to NemesisDevX/flutter that referenced this pull request Nov 6, 2026
flutter/packages@0478162...63881f9

2026-11-06 [email protected] [material_ui] Paint Dialog.fullscreen
background color behind the keyboard (flutter/packages#13244)
2026-11-05 [email protected] [material_ui] Add tabBar/tab
roles and expanded state semantics (flutter/packages#13176)
2026-11-05 [email protected]
[material_ui] Disable the DropdownButtonFormField decoration when the
field is disabled (flutter/packages#13473)
2026-11-05 [email protected] [material_ui] Fix Switch throwing when first
painted mid-animation (flutter/packages#13496)
2026-11-05 [email protected] [various] Add
engineIdentifier to fake plugin registrars (flutter/packages#13574)
2026-11-05 [email protected] [google_sign_in]
Allow GoogleSignIn-iOS 10 (flutter/packages#13306)
2026-11-05 [email protected] [material_ui] Hit
test the floating label of InputDecorator at its painted position
(flutter/packages#13326)
2026-11-05 [email protected] [material_ui] Fix InputDecorator
intrinsic height when isDense or isC… (flutter/packages#13522)
2026-11-05 [email protected] [material_ui] Paint Scaffold
persistent bottom sheet background color behind the keyboard
(flutter/packages#13242)
2026-11-05 [email protected] [image_picker_ios] PR 5/5 Switch
Pigeon to Swift and finish the Objective-C to Swift migration.
(flutter/packages#13493)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC [email protected] on the revert to ensure that a
human
is aware of the problem.

To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App CICD Run CI/CD p: material_ui triage-design Should be looked at in design triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Null check operator used on a null value on Switch _pressedThumbExtension

2 participants