Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Ticker must be disposed before calling super.dispose()

I don't know why this error is appearing in the console box

Console message:

SplashScreenState created a Ticker via its SingleTickerProviderStateMixin, but at the time dispose() was called on the mixin, that Ticker was still active. The Ticker 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. The offending ticker was: Ticker(created by SplashScreenState#dae31(lifecycle state: created))

HERE IS MY FULL CODE FOR SPLASHSCREEN:

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';


class SplashScreen extends StatefulWidget {
  @override
  SplashScreenState createState() => new SplashScreenState();
}

class SplashScreenState extends State<SplashScreen>
    with SingleTickerProviderStateMixin {
  var _visible = true;

  AnimationController animationController;
  Animation<double> animation;

  startTime() async {
    var _duration = new Duration(seconds: 3);
    return new Timer(_duration, navigationPage);
  }

  void navigationPage() {
    Navigator.of(context).pushReplacementNamed(HOME_SCREEN);
  }




  @override
  void initState() {
    super.initState();
    animationController = new AnimationController(
      vsync: this,
      duration: new Duration(seconds: 2),
    );
    animation =
    new CurvedAnimation(parent: animationController, curve: Curves.easeOut);

    animation.addListener(() => this.setState(() {}));
    animationController.forward();

    setState(() {
      _visible = !_visible;
    });
    startTime();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        fit: StackFit.expand,
        children: <Widget>[

          new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              new Image.asset(
                'assets/vegan1.png',
                width: animation.value * 280,
                height: animation.value * 280,
              ),
            ],
          ),
        ],
      ),
    );
  }
}


How can I solve this error. Please answer if you have any solution or idea to solve this.only added important points are added reduce the size of code. If you need more console code then please comment.

like image 327
raman raman Avatar asked Nov 11 '19 13:11

raman raman


People also ask

When should I call my dispose flutter?

Dispose is a method triggered whenever the created object from the stateful widget is removed permanently from the widget tree. It is generally overridden and called only when the state object is destroyed.

Is Dispose method called automatically flutter?

dispose() method called automatically from stateful if not defined. In some cases dispose is required for example in CameraPreview , Timer etc.. you have to close the stream. When closing the stream is required you have to use it in dispose method. dispose() is used to execute code when the screen is disposed.

What is Tickerproviderstatemixin in flutter?

Provides Ticker objects that are configured to only tick while the current tree is enabled, as defined by TickerMode. To create an AnimationController in a class that uses this mixin, pass vsync: this to the animation controller constructor whenever you create a new animation controller.


2 Answers

Override dispose method and dispose the AnimationController instance.

 @override
  void dispose() {
    _animation.dispose();
    _controller.dispose();
    super.dispose();
  }
like image 34
Rahamat Avatar answered Sep 21 '22 08:09

Rahamat


Override dispose method and dispose the AnimationController instance.

@override
dispose() {
  animationController.dispose(); // you need this
  super.dispose();
}
like image 89
CopsOnRoad Avatar answered Sep 20 '22 08:09

CopsOnRoad