Skip to content

[cupertino_ui] Prevent null-check crash during CupertinoTextSelectionToolbar pagination - #13659

Merged
auto-submit[bot] merged 4 commits into
flutter:mainfrom
leizi-google-labs:fix-cupertino-toolbar-pagination
Nov 17, 2026
Merged

auto-submit[bot] merged 4 commits into
flutter:mainfrom
leizi-google-labs:fix-cupertino-toolbar-pagination

Conversation

@leizi-google-labs

Copy link
Copy Markdown
Contributor

Prevents a _TypeError (Null check operator used on a null value at _page = _nextPage!) in _CupertinoTextSelectionToolbarContentState._statusListener when paginating a CupertinoTextSelectionToolbar.

_handleNextPage and _handlePreviousPage previously called _controller.addStatusListener(_statusListener) unconditionally onto AnimationController's ObserverList (which permits duplicate listeners, while removeStatusListener removes only a single copy). When a user taps the pagination chevron or swipes horizontally more than once before the 125ms reverse transition finishes (or if didUpdateWidget resets _nextPage while duplicate listeners remain registered), notifyStatusListeners invokes _statusListener a second time after the first call has already consumed _nextPage and set it to null.

This PR removes any existing _statusListener registration before adding it in _handleNextPage and _handlePreviousPage, and guards against a null _nextPage in _statusListener before calling setState.

Fixes b/583370306

Pre-Review Checklist

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

@github-actions github-actions Bot added p: cupertino_ui triage-design Should be looked at in design triage labels Nov 13, 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 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.

Comment on lines 589 to 592
_controller.reverse();
_controller.removeStatusListener(_statusListener);
_controller.addStatusListener(_statusListener);
_nextPage = _page + 1;

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.

medium

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();

Comment on lines 601 to 604
_controller.reverse();
_controller.removeStatusListener(_statusListener);
_controller.addStatusListener(_statusListener);
_nextPage = _page - 1;

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.

medium

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();

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.

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);
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @Piinks! Updated to set _nextPage and register _statusListener before calling _controller.reverse(), keep _controller.forward() unconditional in _statusListener, and added the test case.

Comment on lines +613 to 623
_controller.removeStatusListener(_statusListener);
final int? nextPage = _nextPage;
if (nextPage == null) {
return;
}

setState(() {
_page = _nextPage!;
_page = nextPage;
_nextPage = null;
});
_controller.forward();

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.

medium

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();

@Piinks Piinks added the CICD Run CI/CD label Nov 13, 2026

@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.

Thanks for fixing this!

Comment on lines 601 to 604
_controller.reverse();
_controller.removeStatusListener(_statusListener);
_controller.addStatusListener(_statusListener);
_nextPage = _page - 1;

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.

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);
});

@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Nov 16, 2026
@Piinks Piinks added the CICD Run CI/CD label Nov 16, 2026

@dkwingsmt dkwingsmt 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.

RSLGTM

@leizi-google-labs

Copy link
Copy Markdown
Contributor Author

Thanks for the review and approval @dkwingsmt @Piinks! All checks are green—could one of you please add the autosubmit label (or merge) as I don't have label permissions on this repo yet? Thank you!

@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

@Piinks

Piinks commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

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. 😅

@Piinks
Piinks requested a review from pjkammer November 17, 2026 01:08
@Piinks Piinks added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 17, 2026
@auto-submit
auto-submit Bot merged commit 990d281 into flutter:main Nov 17, 2026
14 checks passed
@Piinks

Piinks commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Guess not, all good! Thanks for sending the fix!

ditman pushed a commit to ditman/flutter-flutter that referenced this pull request Nov 17, 2026
…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
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: cupertino_ui triage-design Should be looked at in design triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants