How to dispose tickers?

Viewed 2319

_LocationNotEnabledState#340f0(tickers: tracking 2 tickers) was disposed with an active Ticker.

_LocationNotEnabledState created a Ticker via its TickerProviderStateMixin, but at the time dispose() was called on the mixin, that Ticker was still active. All Tickers must be disposed before calling super.dispose().

Tickers used by AnimationControllers should be disposed by calling dispose() on the AnimationController itself. Otherwise, the ticker will leak.

3 Answers

You can override the dispose method of your page. And then you can dispose your animation controller like this:

@override
dispose() {
  animationController.dispose();
  super.dispose();
}

I suggest you to use flutter_hooks package which manages a widget life-cycle, so you don't have to worry about dispose methods anymore. The package comes with a list of reusable hooks already provided, including TickerProvider and AnimationController.

@override
void dispose() {
  super.dispose();
  animationController.dispose();
}
Related