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

setState called after dispose #8

Closed
modulovalue opened this issue Sep 13, 2018 · 7 comments · Fixed by #13
Closed

setState called after dispose #8

modulovalue opened this issue Sep 13, 2018 · 7 comments · Fixed by #13

Comments

@modulovalue
Copy link
Contributor

modulovalue commented Sep 13, 2018

flutter: The following assertion was thrown while notifying status listeners for AnimationController:
flutter: setState() called after dispose(): _VillainState#f0d79(lifecycle state: defunct, not mounted)
flutter: This error happens if you call setState() on a State object for a widget that no longer appears in
flutter: the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error
flutter: can occur when code calls setState() from a timer or an animation callback. The preferred solution
flutter: is to cancel the timer or stop listening to the animation in the dispose() callback. Another
flutter: solution is to check the "mounted" property of this object before calling setState() to ensure the
flutter: object is still in the tree.
flutter: This error might indicate a memory leak if setState() is being called because another object is
flutter: retaining a reference to this State object after it has been removed from the tree. To avoid memory
flutter: leaks, consider breaking the reference to this object during dispose().
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      State.setState.<anonymous closure> (package:flutter/src/widgets/framework.dart:1098:9)
flutter: #1      State.setState (package:flutter/src/widgets/framework.dart:1124:6)
flutter: #2      _VillainState._handleStatusChange (package:flutter_villains/villains/villains.dart:129:9)
flutter: #3      _AnimationController&Animation&AnimationEagerListenerMixin&AnimationLocalListenersMixin&AnimationLocalStatusListenersMixin.notifyStatusListeners (package:flutter/src/animation/listener_helpers.dart:161:19)
flutter: #4      AnimationController._checkStatusChanged (package:flutter/src/animation/animation_controller.dart:593:7)
flutter: #5      AnimationController._tick (package:flutter/src/animation/animation_controller.dart:609:5)
flutter: #6      Ticker._tick (package:flutter/src/scheduler/ticker.dart:228:5)
flutter: #7      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
flutter: #8      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleBeginFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:906:11)
flutter: #9      __InternalLinkedHashMap&_HashVMBase&MapMixin&_LinkedHashMapMixin.forEach (dart:collection/runtime/libcompact_hash.dart:365:8)
flutter: #10     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleBeginFrame (package:flutter/src/scheduler/binding.dart:904:17)
flutter: #11     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleBeginFrame (package:flutter/src/scheduler/binding.dart:834:5)
flutter: #12     _invoke1 (dart:ui/hooks.dart:142:13)
flutter: #13     _beginFrame (dart:ui/hooks.dart:113:3)
flutter:
flutter: The AnimationController notifying status listeners was:
flutter:   AnimationController#9085f(⏭ 1.000; paused)```
@modulovalue
Copy link
Contributor Author

relevant line:

_VillainState._handleStatusChange (package:flutter_villains/villains/villains.dart:129:9)

@Norbert515
Copy link
Owner

What kind of VillainAnimation did you use? How can I reproduce this?

@modulovalue
Copy link
Contributor Author

Try closing a widget in which a villain animation is still running

@Timouri
Copy link

Timouri commented Feb 19, 2019

Before each:
setState((){});
add:
if (!mounted) return;

@ACE-YANGCE
Copy link

if (this.mounted) {
setState(() {});
}

modulovalue added a commit to modulovalue/flutter_villains that referenced this issue Apr 30, 2019
modulovalue added a commit to modulovalue/flutter_villains that referenced this issue Apr 30, 2019
@spkersten
Copy link
Contributor

spkersten commented May 11, 2019

@modulovalue I've looked at the fix in the commits reference above. Wouldn't it be cleaner to remove the status listener from _controllerAnimation in _VillainState.dispose() instead of the if (mounted) in _handleStatusChange?

@KurdAppDev
Copy link

@override
   void setState(fn) {
    if(mounted){
      super.setState(fn);
    }
  }

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.

6 participants