动画控制器异常为空:在空时调用了 getter 'isAnimating'?



我构建了一个具有两个屏幕的应用程序。第一个屏幕是动画,第二个屏幕是设置一些属性。

第一个带有动画的屏幕:

class _RotatingCirclePageState extends State<RotatingCirclePage>
with SingleTickerProviderStateMixin {
AnimationController animationController;
Animation animation;
@override
void initState() {
super.initState();
animationController = AnimationController(
duration: Duration(
seconds: widget.inspi,
),
vsync: this,
);
animation = CurvedAnimation(
parent: animationController,
curve: Curves.linear,
);
}
...
void go(){
if(animationController.isAnimating){ // ---> Throw Exception 
animationController.stop();
}
...
}
...
}

它就像一个符咒。启动应用程序,点击屏幕,动画开始。。。转到第二个屏幕(使用"导航器"(,设置属性。返回到第一个屏幕(使用"导航器"(,animationController为null。

EXCEPTION CAUGHT BY GESTURE 
I/flutter ( 5888): The following NoSuchMethodError was thrown while handling a gesture:
I/flutter ( 5888): The getter 'isAnimating' was called on null.
I/flutter ( 5888): Receiver: null
I/flutter ( 5888): Tried calling: isAnimating

转到屏幕1到屏幕2的按钮

RaisedButton(
child: Text('...'),
onPressed: () {
Navigator.of(context)
.push(MaterialPageRoute<Null>(builder: (BuildContext context) {
return new SliderExample();
}));
}),

转到屏幕2到屏幕1的按钮

RaisedButton(
child: Text('Go Back'),
onPressed: () {
_save();
Navigator.pop(context);
},
),

我尝试了您提供的代码,没有任何错误。

添加更多相关代码,例如使用动画

您是如何以及在哪里调用go()函数的?

顺便问一下,您是否正在处理动画控制器

在你的第一页你必须有这个

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

最新更新