Repository navigation
[cupertino_ui] Prevent null-check crash during CupertinoTextSelectionToolbar pagination - #13659
Conversation
…Toolbar pagination
There was a problem hiding this comment.
Code Review
This pull request addresses an issue in CupertinoTextSelectionToolbar where rapid pagination during a page transition animation throws an exception. It updates the animation controller's status listener management and adds a test to verify the fix. The review feedback suggests ordering state and listener setup before triggering the reverse animation to avoid stale state, and ensuring the forward animation is called even if the next page is null to prevent the toolbar from remaining permanently faded out.
| _controller.reverse(); | ||
| _controller.removeStatusListener(_statusListener); | ||
| _controller.addStatusListener(_statusListener); | ||
| _nextPage = _page + 1; |
There was a problem hiding this comment.
It is safer to set up the state (_nextPage) and listeners before starting the animation (_controller.reverse()). This prevents any potential synchronous listener notifications from running with stale state or missing listeners.
_nextPage = _page + 1;
_controller.removeStatusListener(_statusListener);
_controller.addStatusListener(_statusListener);
_controller.reverse();| _controller.reverse(); | ||
| _controller.removeStatusListener(_statusListener); | ||
| _controller.addStatusListener(_statusListener); | ||
| _nextPage = _page - 1; |
There was a problem hiding this comment.
It is safer to set up the state (_nextPage) and listeners before starting the animation (_controller.reverse()). This prevents any potential synchronous listener notifications from running with stale state or missing listeners.
_nextPage = _page - 1;
_controller.removeStatusListener(_statusListener);
_controller.addStatusListener(_statusListener);
_controller.reverse();There was a problem hiding this comment.
This is a good suggestion. Here is a test case for it!
Details
testWidgets('paginating right as reverse transition completes does not leave toolbar invisible', (
WidgetTester tester,
) async {
final children = List<Widget>.generate(15, (int i) => const TestBox());
await tester.pumpWidget(
CupertinoApp(
home: Center(
child: CupertinoTextSelectionToolbar(
anchorAbove: const Offset(50.0, 100.0),
anchorBelow: const Offset(50.0, 200.0),
children: children,
),
),
),
);
await tester.tapAt(tester.getCenter(findOverflowNextButton()));
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
final TestGesture gesture = await tester.startGesture(
tester.getCenter(findOverflowNextButton()),
);
await tester.pump(const Duration(milliseconds: 25));
await gesture.moveBy(const Offset(-50.0, 0.0), timeStamp: const Duration(milliseconds: 25));
await tester.pump(const Duration(milliseconds: 51));
await gesture.moveBy(const Offset(-50.0, 0.0), timeStamp: const Duration(milliseconds: 60));
await gesture.up(timeStamp: const Duration(milliseconds: 76));
await tester.pumpAndSettle();
final FadeTransition fade = tester.widget<FadeTransition>(
find.ancestor(
of: find.byType(AnimatedSize),
matching: find.byType(FadeTransition),
),
);
expect(fade.opacity.value, 1.0);
expect(findOverflowNextButton(), findsNothing);
expect(findOverflowBackButton(), findsOneWidget);
});There was a problem hiding this comment.
Thanks @Piinks! Updated to set _nextPage and register _statusListener before calling _controller.reverse(), keep _controller.forward() unconditional in _statusListener, and added the test case.
| _controller.removeStatusListener(_statusListener); | ||
| final int? nextPage = _nextPage; | ||
| if (nextPage == null) { | ||
| return; | ||
| } | ||
|
|
||
| setState(() { | ||
| _page = _nextPage!; | ||
| _page = nextPage; | ||
| _nextPage = null; | ||
| }); | ||
| _controller.forward(); |
There was a problem hiding this comment.
If _nextPage is null, returning early without calling _controller.forward() will leave the toolbar in a fully faded-out (invisible) state, making it permanently unusable. Instead of returning early, we should still call _controller.forward() to ensure the toolbar fades back in on the current page if the transition was aborted or invalidated.
_controller.removeStatusListener(_statusListener);
final int? nextPage = _nextPage;
if (nextPage != null) {
setState(() {
_page = nextPage;
_nextPage = null;
});
}
_controller.forward();| _controller.reverse(); | ||
| _controller.removeStatusListener(_statusListener); | ||
| _controller.addStatusListener(_statusListener); | ||
| _nextPage = _page - 1; |
There was a problem hiding this comment.
This is a good suggestion. Here is a test case for it!
Details
testWidgets('paginating right as reverse transition completes does not leave toolbar invisible', (
WidgetTester tester,
) async {
final children = List<Widget>.generate(15, (int i) => const TestBox());
await tester.pumpWidget(
CupertinoApp(
home: Center(
child: CupertinoTextSelectionToolbar(
anchorAbove: const Offset(50.0, 100.0),
anchorBelow: const Offset(50.0, 200.0),
children: children,
),
),
),
);
await tester.tapAt(tester.getCenter(findOverflowNextButton()));
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
final TestGesture gesture = await tester.startGesture(
tester.getCenter(findOverflowNextButton()),
);
await tester.pump(const Duration(milliseconds: 25));
await gesture.moveBy(const Offset(-50.0, 0.0), timeStamp: const Duration(milliseconds: 25));
await tester.pump(const Duration(milliseconds: 51));
await gesture.moveBy(const Offset(-50.0, 0.0), timeStamp: const Duration(milliseconds: 60));
await gesture.up(timeStamp: const Duration(milliseconds: 76));
await tester.pumpAndSettle();
final FadeTransition fade = tester.widget<FadeTransition>(
find.ancestor(
of: find.byType(AnimatedSize),
matching: find.byType(FadeTransition),
),
);
expect(fade.opacity.value, 1.0);
expect(findOverflowNextButton(), findsNothing);
expect(findOverflowBackButton(), findsOneWidget);
});|
Thanks for the review and approval @dkwingsmt @Piinks! All checks are green—could one of you please add the |
|
I think we need another approval here, 2 are required, and I pushed to this to fix some formatting issues to save you the trip - which probably means mine does not count. Working on getting another review. 😅 |
|
Guess not, all good! Thanks for sending the fix! |
…r#196812) flutter/packages@4c6a7d3...9af69be 2026-11-17 [email protected] [in_app_purchase_storekit] add warm up test to preaction (flutter/packages#13661) 2026-11-17 [email protected] Roll Flutter from d8bd864 to 2f09f96 (27 revisions) (flutter/packages#13695) 2026-11-17 [email protected] [url_launcher] Convert implementation to Kotlin (flutter/packages#13663) 2026-11-17 [email protected] [cupertino_ui] Prevent null-check crash during CupertinoTextSelectionToolbar pagination (flutter/packages#13659) 2026-11-17 [email protected] [ci] Allow backfilling missed releases via workflow_dispatch (flutter/packages#13612) 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
Prevents a
_TypeError(Null check operator used on a null valueat_page = _nextPage!) in_CupertinoTextSelectionToolbarContentState._statusListenerwhen paginating aCupertinoTextSelectionToolbar._handleNextPageand_handlePreviousPagepreviously called_controller.addStatusListener(_statusListener)unconditionally ontoAnimationController'sObserverList(which permits duplicate listeners, whileremoveStatusListenerremoves only a single copy). When a user taps the pagination chevron or swipes horizontally more than once before the 125ms reverse transition finishes (or ifdidUpdateWidgetresets_nextPagewhile duplicate listeners remain registered),notifyStatusListenersinvokes_statusListenera second time after the first call has already consumed_nextPageand set it tonull.This PR removes any existing
_statusListenerregistration before adding it in_handleNextPageand_handlePreviousPage, and guards against a null_nextPagein_statusListenerbefore callingsetState.Fixes b/583370306
Pre-Review Checklist
[shared_preferences]///).Footnotes
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