Skip to content

Commit 8a74ca8

Browse files
[cupertino_ui] Fix CupertinoPicker throwing when disposed during the tap-to-select animation (#13012)
Tapping an item that isn't centred starts a 300 ms tap-to-select animation, which `_CupertinoPickerState._handleChildTap` awaits and then reads `_effectiveController.selectedItem`. If the picker is disposed before that animation finishes, the controller has already been detached, so the read fails: an assertion in debug (`FixedExtentScrollController.selectedItem cannot be accessed before a scroll view is built with it.`) and `StateError: Bad state: No element` in release. It shows up in ordinary "select, then hide" patterns — a parent that removes the picker as soon as a value changes, or a Material bottom sheet (200 ms exit) popped right after a tap. The reporter's analysis in the issue pinned it to this exact line and supplied a deterministic reproduction; credit to them. The fix returns early after the `await` when the state is no longer mounted. Nothing after that point matters for a disposed picker — it only restores haptic bookkeeping. The new test taps an off-centre item, removes the picker 50 ms into the animation, and pumps past the end. It fails on `main` with the assertion above and passes with the fix. The rest of `picker_test.dart` (28) and `date_picker_test.dart` (85), which covers the pickers built on top of `CupertinoPicker`, pass as well. Fixes flutter/flutter#192135 ## Pre-Review Checklist I used AI tooling while working on this, and reviewed and tested all of it myself. [^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.
1 parent 78b120c commit 8a74ca8

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

‎packages/cupertino_ui/lib/src/picker.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,12 @@ class _CupertinoPickerState extends State<CupertinoPicker> {
323323
duration: _kCupertinoPickerTapToScrollDuration,
324324
curve: _kCupertinoPickerTapToScrollCurve,
325325
);
326+
// The picker can be disposed while the animation above is running, for
327+
// example when a parent hides it as soon as a value is selected. Its
328+
// controller is detached by then, and reading selectedItem would throw.
329+
if (!mounted) {
330+
return;
331+
}
326332
_enableHapticFeedback = true;
327333
_lastHapticIndex = _effectiveController.selectedItem;
328334
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes `CupertinoPicker` throwing when it is disposed during the tap-to-select animation.
3+
version: patch

‎packages/cupertino_ui/test/picker_test.dart‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,4 +949,52 @@ void main() {
949949
);
950950
expect(tester.getSize(find.byType(CupertinoPicker)), Size.zero);
951951
});
952+
953+
testWidgets('Does not throw when disposed during the tap-to-select animation', (
954+
WidgetTester tester,
955+
) async {
956+
// Regression test for https://github.com/flutter/flutter/issues/192135
957+
var showPicker = true;
958+
late StateSetter setOuterState;
959+
960+
await tester.pumpWidget(
961+
CupertinoApp(
962+
home: StatefulBuilder(
963+
builder: (BuildContext context, StateSetter setState) {
964+
setOuterState = setState;
965+
return Center(
966+
child: showPicker
967+
? SizedBox(
968+
height: 216,
969+
child: CupertinoPicker(
970+
itemExtent: 32,
971+
onSelectedItemChanged: (int index) {},
972+
children: List<Widget>.generate(
973+
20,
974+
(int index) => Center(child: Text('Item $index')),
975+
),
976+
),
977+
)
978+
: const SizedBox.shrink(),
979+
);
980+
},
981+
),
982+
),
983+
);
984+
985+
// Tapping an item that is not centred starts the tap-to-select
986+
// animation, which _handleChildTap awaits.
987+
await tester.tap(find.text('Item 2'));
988+
await tester.pump();
989+
await tester.pump(const Duration(milliseconds: 50));
990+
991+
// Remove the picker while that animation is still running.
992+
setOuterState(() {
993+
showPicker = false;
994+
});
995+
await tester.pump();
996+
await tester.pump(const Duration(milliseconds: 500));
997+
998+
expect(tester.takeException(), isNull);
999+
});
9521000
}

0 commit comments

Comments
 (0)