flutter - Flutter:必须在调用 super.dispose() 之前处理 Ticker
问题描述
我不知道为什么控制台框中会出现此错误
控制台消息:
SplashScreenState 通过其 SingleTickerProviderStateMixin 创建了一个 Ticker,但在 mixin 上调用 dispose() 时,该 Ticker 仍然处于活动状态。Ticker 必须在调用 super.dispose() 之前被释放。AnimationControllers 使用的 Ticker 应该通过在 AnimationController 本身上调用 dispose() 来处理。否则,自动收报机将泄漏。有问题的代码是:代码(由 SplashScreenState#dae31(生命周期状态:创建)创建)
这是我的启动画面的完整代码:
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();
}
推荐阅读
- javascript - 使用组件实现 Redux-Search
- jmeter - 更新 JMeter 中同一请求的每次出现的参数值
- c# - Web API 不返回特定的实体列表
- python - Pandas:如果单元格包含特定文本,则删除行
- php - 使用嵌套 foreach 编写此 if-else 的更简单方法
- python - PyGame - 如何在屏幕中心获取图像
- python - 在 Heroku 上安装 Azure pyodbc 驱动程序
- javascript - 如何通过两个组件传递我的函数?
- typescript - 使用 TS 编译后,导入类在文件之间不起作用
- wpf - 在视觉状态下更改标签前景色