Skip to content

Null check operator used on a null value on Switch _pressedThumbExtension #179532

Closed
flutter/packages
#13496

Description

@ostk0069

Steps to reproduce

_pressedInactiveThumbRadius! * 2 + _pressedThumbExtension!,
uses bang operator in _pressedThumbExtension . However, _pressedThumbExtension is nullable and _pressedThumbExtension insert value only when _stopPressAnimation is false.

Expected results

_pressedThumbExtension should be 0 by default

Actual results

_pressedThumbExtension should be 0 by default

Code sample

Code sample
...
// BEFORE: late double? _pressedThumbExtension;
double _pressedThumbExtension = 0;
...
// BEFORE: _pressedInactiveThumbRadius! * 2 + _pressedThumbExtension!,
_pressedInactiveThumbRadius! * 2 + _pressedThumbExtension,

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[Paste your output here]

Activity

  1. added
    in triagePresently being triaged by the triage team
    p: material_uimaterial_ui package in flutter/packages
    frameworkflutter/packages/flutter repository. See also f: labels.
    team-designOwned by Design Languages team
    and removed
    in triagePresently being triaged by the triage team
    on Dec 8, 2025
  2. dkwingsmt commented on Dec 10, 2025

    @dkwingsmt
    Contributor

    It would be more helpful if you can provide a reproduction that triggers this crash. This issue can use such a production for future tracking, and your PR will certainly need this reproduction for regression test.

  3. added
    P2Important issues not at the top of the work list
    triaged-designTriaged by Design Languages team
    on Dec 10, 2025
  4. ishaquehassan commented on Feb 27, 2026

    @ishaquehassan
    Contributor

    Looking at packages/flutter/lib/src/material/switch.dart, the crash comes from _pressedThumbExtension being declared as late double? (line 1507) but only assigned inside the if (!_stopPressAnimation) block (line 1526). When _stopPressAnimation is true (which happens when reaction.status == AnimationStatus.reverse on the very first paint call after a touch lifts), the field is never initialized.

    _computeThumbPaintOffset uses _pressedThumbExtension! unconditionally at lines 1720 and 1724, regardless of _stopPressAnimation. So on the first paint() with _stopPressAnimation = true, you get a LateInitializationError.

    The fix is to drop late and give the field a default value of 0 instead:

    // Before:
    late double? _pressedThumbExtension;
    
    // After:
    double _pressedThumbExtension = 0;

    Then remove the ! in _computeThumbPaintOffset (lines 1720, 1724), since the field is no longer nullable. The isCupertino paths at lines 1559, 1565, and 1616 that use _pressedThumbExtension! are already inside the if (!_stopPressAnimation) guard, so they just become plain _pressedThumbExtension after the type change.

    This guarantees the field always has a valid value before _computeThumbPaintOffset runs, regardless of animation state or frame timing.

  5. added
    packageflutter/packages repository. See also p: labels.
    and removed
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listp: material_uimaterial_ui package in flutter/packagespackageflutter/packages repository. See also p: labels.team-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions