Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timer not disposed when the animated text is #98

Closed
Brainstorm67 opened this issue Jun 27, 2020 · 1 comment · Fixed by #121
Closed

timer not disposed when the animated text is #98

Brainstorm67 opened this issue Jun 27, 2020 · 1 comment · Fixed by #121

Comments

@Brainstorm67
Copy link

When using FadeAnimatedTextKit (and probably other animations to check) the inner timer keeps going even if the widget is disposed (on screen change for example).
This can cause trouble depending on what is in the onFinished callback. In my case it was a setState going crazy because its widget was already disposed.

To Reproduce
my usage :
FadeAnimatedTextKit(
duration: Duration(milliseconds: 500),
isRepeatingAnimation: false,
onFinished: () {
setState(() {
go = true;
});
},
text: ['Ready ?', '3', '2', '1', 'Go !'],
textStyle: kCenterStyle,
)

the error triggered when I swipe pages while text is animating :
[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: setState() called after dispose(): _StartScreenState#2530f(lifecycle state: defunct, not mounted)
E/flutter (26541): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.
E/flutter (26541): The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
E/flutter (26541): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().

Expected behavior
The timer should be canceled on dispose

Flutter:

  • version 1.19.0-4.2.pre
  • channel beta

Dart:

  • version 2.9.0 (build 2.9.0-14.1.beta)
@awhitford
Copy link
Collaborator

I think you are right in that the Timer's lifecycle does not seem to be properly managed.

At the very least, I think calling _timer?.cancel(); in the dispose() method is worth adding. Not totally sure if that is all, though -- I'd like to see some kind of assertion before creating a new _timer that there isn't already an active timer (otherwise it will leak).

aagarwal1012 added a commit that referenced this issue Oct 12, 2020
Ensured that the timer has been canceled. Resolves #98.
@aagarwal1012 aagarwal1012 linked a pull request Oct 14, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants