Skip to content

Update TickerMode.getValuesNotifier to handle initialization during State.dispose#185248

Merged
auto-submit[bot] merged 4 commits into
flutter:masterfrom
nate-thegrate:late-animation
Apr 29, 2026
Merged

Update TickerMode.getValuesNotifier to handle initialization during State.dispose#185248
auto-submit[bot] merged 4 commits into
flutter:masterfrom
nate-thegrate:late-animation

Conversation

@nate-thegrate

@nate-thegrate nate-thegrate commented Apr 18, 2026

Copy link
Copy Markdown
Contributor

Currently, TickerMode.getValuesNotifier assumes that getInheritedWidgetOfExactType is safe to call, but there's an edge case where a widget defines an animation controller field and then is disposed of before that field is referenced elsewhere.

This PR updates the logic in getValuesNotifier() to handle that situation instead of throwing an error.

related to #153644

@github-actions github-actions Bot added the framework flutter/packages/flutter repository. See also f: labels. label Apr 18, 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 updates TickerMode.getValuesNotifier to return a fallback value when the context is unmounted, preventing assertion errors that occur when late final animation controllers are disposed. It also adds regression tests for SingleTickerProviderStateMixin and TickerProviderStateMixin. The review feedback suggests removing the kDebugMode check to simplify the logic and ensure consistent behavior across all build modes.

// The getInheritedWidgetOfExactType() method throws an assertion error if called during
// State.dispose(), which becomes a problem when an animation controller is set as a
// late final class member and isn't referenced until then.
if (kDebugMode && !context.mounted) {

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

The kDebugMode check is not strictly necessary here. If context.mounted is false, the element is defunct and getInheritedWidgetOfExactType would return null in release mode anyway (after a more expensive lookup). Removing kDebugMode simplifies the logic and provides a consistent, slightly more efficient path in all modes when the context is unmounted. This also aligns with the general practice of using context.mounted to guard against using a defunct context.

    if (!context.mounted) {
References
  1. Optimize for readability and avoid unnecessary complexity. (link)

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.

SGTM :)

const fallback = _ConstantTickerModeDataListenable(TickerModeData.fallback);

// The getInheritedWidgetOfExactType() method throws an assertion error if called during
// State.dispose(), which becomes a problem when an animation controller is set as a

@navaronbracke navaronbracke Apr 20, 2026

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 technically not tied to animation controller, but the consumer of the VSync? (the class that mixes in the ticker provider)

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.

When getValuesNotifier is called during State.dispose, I guess you could say the call is tied to both the animation controller and the State subclass that mixes in the ticker provider.

When the animation controller object is being referenced for the first time, the AnimationController constructor is called, which then calls the ticker provider's createTicker method, and inside that method is where TickerMode.getValuesNotifier is eventually called.

Please let me know if I misunderstood your question!

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

I would normally think we should throw the error anyways. But the fact that getValuesNotifier already has a fallback mechanism makes me think this PR is 👍

// The getInheritedWidgetOfExactType() method throws an assertion error if called during
// State.dispose(), which becomes a problem when an animation controller is set as a
// late final class member and isn't referenced until then.
if (!context.mounted) {

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.

context.getInheritedWidgetOfExactType throws if the current lifecycle state is not _ElementLifecycle.active. Is this == context.mounted?

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.

That's a good question!

If we land this change, hypothetically TickerMode.getValuesNotifier could still throw an error if a late animation controller is referenced for the first time while an element is in the middle of GlobalKey re-parenting.

But realistically, I don't think this a cause for concern.

@victorsanni victorsanni added the CICD Run CI/CD label Apr 24, 2026

@justinmc justinmc 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 👍 . Is this the only thing blocking #153644? I think I missed that issue when you first created it, but reading through it now, I'm cautiously excited about it. It really simplifies things in the examples you gave at least.

@nate-thegrate

Copy link
Copy Markdown
Contributor Author

Is this the only thing blocking #153644?

I think so! I believe we can do a really nice cleanup once this PR lands.
The point made in #153644 (comment) still stands though; we'd need to consider potential side effects of late initialization, for example:

late final controller = AnimationController(vsync: this)..forward();

The forward() call would probably need to be moved to initState to ensure that it's actually called when we expect it to be.

@justinmc justinmc added the autosubmit Merge PR when tree becomes green via auto submit App label Apr 29, 2026
@auto-submit auto-submit Bot added this pull request to the merge queue Apr 29, 2026
Merged via the queue into flutter:master with commit 143c87d Apr 29, 2026
87 of 88 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Apr 29, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request May 1, 2026
Roll Flutter from 81bc3d69535f to 707dbc0420a3 (85 revisions)

flutter/flutter@81bc3d6...707dbc0

2026-05-01 [email protected] Removing TODOs from the WebParagraph code and addressing technical debts. (flutter/flutter#185168)
2026-05-01 [email protected] Ensure that vulkan_interface.h gets included before vk_mem_alloc.h (flutter/flutter#185777)
2026-05-01 [email protected] [flutter_tools] Bump dwds dependency to v27.1.1 (flutter/flutter#185357)
2026-05-01 [email protected] Roll Dart SDK from 6d4a319cbdac to 9aa7097f2e3e (3 revisions) (flutter/flutter#185888)
2026-05-01 [email protected] Roll Skia from fa1dcb289709 to 7ac6d42f2fd0 (1 revision) (flutter/flutter#185887)
2026-05-01 [email protected] Roll Skia from 54cc00adde38 to fa1dcb289709 (3 revisions) (flutter/flutter#185880)
2026-05-01 [email protected] [iOS] Migrate to FlutterFMLTaskRunner(s) (flutter/flutter#185815)
2026-05-01 [email protected] Remove material imports from navigator_on_did_remove_page_test and scrollable_in_overlay_test (flutter/flutter#182546)
2026-05-01 [email protected] Roll Skia from 2e279266f06a to 54cc00adde38 (3 revisions) (flutter/flutter#185872)
2026-05-01 [email protected] dev: Remove unused parameters (flutter/flutter#185345)
2026-05-01 [email protected] Roll Fuchsia Linux SDK from HN5VYzftnf_B8T-n9... to VnzuUefDQR0UhQ1L1... (flutter/flutter#185870)
2026-05-01 [email protected] Use g_free when using glib memory allocation (flutter/flutter#185519)
2026-05-01 [email protected] Roll Dart SDK from d30df3428f2e to 6d4a319cbdac (2 revisions) (flutter/flutter#185862)
2026-05-01 [email protected] Remove trivial test utility cross-imports from material and cupertino… (flutter/flutter#184295)
2026-05-01 [email protected] Inline test callback painter in tab scaffold test (flutter/flutter#184851)
2026-05-01 [email protected] [a11y] Add support for high contrast themes in the a11y assessments app  (flutter/flutter#185801)
2026-05-01 [email protected] [a11y assessment app] Use default color for banner (flutter/flutter#185804)
2026-04-30 [email protected] Added name to AUTHORS (flutter/flutter#185586)
2026-04-30 [email protected] Remove semantics_tester import from raw_material_button_test.dart (flutter/flutter#184806)
2026-04-30 [email protected] Remove semantics_tester import from user_accounts_drawer_header_test.dart (flutter/flutter#184809)
2026-04-30 [email protected] Roll Skia from 7b88c5c281e5 to 2e279266f06a (5 revisions) (flutter/flutter#185854)
2026-04-30 [email protected] Handle symmetric rectangular and elliptical round super ellipses in the uber SDF renderer  (flutter/flutter#185695)
2026-04-30 [email protected] Match on process name before killing for SwiftPM (flutter/flutter#185774)
2026-04-30 [email protected] Sync CHANGELOG.md from stable (flutter/flutter#185838)
2026-04-30 [email protected] Roll Dart SDK from 25910e31a6d2 to d30df3428f2e (5 revisions) (flutter/flutter#185839)
2026-04-30 [email protected] Check cross imports test subfolders (flutter/flutter#185493)
2026-04-30 [email protected] test: inline TestCallbackPainter in cupertino/picker_test.dart (flutter/flutter#185398)
2026-04-30 [email protected] Update customer testing version (flutter/flutter#185830)
2026-04-30 [email protected] Adapt the Metal shader library output list for debug versus release mode (flutter/flutter#185798)
2026-04-30 [email protected] [Impeller] Port a recent Vulkan swapchain fence waiting fix to the AHB version of the swapchain (flutter/flutter#185763)
2026-04-30 [email protected] Roll Skia from 26a59aa71eff to 7b88c5c281e5 (1 revision) (flutter/flutter#185821)
2026-04-30 [email protected] Roll Skia from 6b4167b4e204 to 26a59aa71eff (4 revisions) (flutter/flutter#185808)
2026-04-30 [email protected] [a11y] Mark SemanticsNode dirty when customSemanticsActions change  (flutter/flutter#185707)
2026-04-30 [email protected] Roll Skia from 1bd2f1cc2746 to 6b4167b4e204 (8 revisions) (flutter/flutter#185799)
2026-04-30 [email protected] [iOS] Extract FlutterVSyncClient from vsync_waiter_ios (flutter/flutter#185737)
2026-04-30 [email protected] Roll Fuchsia Linux SDK from nnv8-SSam6yE8dw4z... to HN5VYzftnf_B8T-n9... (flutter/flutter#185782)
2026-04-29 [email protected] [iOS] Soften TaskRunner.postTask(delay:task:) delay check (flutter/flutter#185729)
2026-04-29 [email protected] Add reportErrors to ImageStreamListener (flutter/flutter#180327)
2026-04-29 [email protected] Roll Skia from f5c781c083c7 to 1bd2f1cc2746 (5 revisions) (flutter/flutter#185761)
2026-04-29 [email protected] Update merge semantics logic to merge sibling nodes (flutter/flutter#183745)
2026-04-29 [email protected] Roll Packages from ba80f8f to cde5b36 (12 revisions) (flutter/flutter#185748)
2026-04-29 [email protected] examples: Remove unused parameters (flutter/flutter#185346)
2026-04-29 [email protected] Update TickerMode.getValuesNotifier to handle initialization during State.dispose (flutter/flutter#185248)
2026-04-29 [email protected] Update triage links (flutter/flutter#185714)
2026-04-29 [email protected] Add support for high contrast and color inversion on Android (flutter/flutter#182263)
2026-04-29 [email protected] Roll Skia from 05251260fda6 to f5c781c083c7 (2 revisions) (flutter/flutter#185743)
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants