gpt4 book ai didi

Flutter:在调用 super.dispose() 之前必须处理 Ticker

转载 作者:行者123 更新时间:2023-12-03 02:39:25 28 4
gpt4 key购买 nike

我不知道为什么控制台框中会出现此错误

控制台消息:

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))



这是我的启动画面完整代码:
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,
),
],
),
],
),
);
}
}



我该如何解决这个错误。如果您有解决此问题的任何解决方案或想法,请回答。仅添加重要的点以减少代码的大小。如果您需要更多控制台代码,请发表评论。

最佳答案

覆盖 dispose方法和处置AnimationController实例。

@override
dispose() {
animationController.dispose(); // you need this
super.dispose();
}

关于Flutter:在调用 super.dispose() 之前必须处理 Ticker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58802223/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com