こんにちは、Flutter赤ん坊です。
今回は、アニメーションをディレイした時にでてくる、こんなやつです。
Failed assertion: line 455 pos 7: '_ticker != null': AnimationController.forward() called after AnimationController.dispose()
要は、アニメーションをものすごい事にしたくて一つ一つにディレイをかけたら、アニメーションの始まる頃にはすでにコントローラーがお亡くなりになっていた状態ですね。
具体的には、ディレイは次のように仕込みます。
Duration dur = Duration(milliseconds: index * 50);
Future.delayed(dur, _controller.forward);
すると次のようになります。
関数実行 ---> index * 50ms待つ
---> アニメーション開始(待っていた間にコントローラーが死亡)
回避策
これを回避するためには、次のように書きます。mountedを用います。
Duration dur = Duration(milliseconds: index * 50);
Future.delayed(dur, start);
void start(){
if(!mounted){
return;
}
_controller.forward
}
mountedは、以下のように説明されています。
bool get mounted
package:flutter/src/widgets/framework.dart
Whether this [State] object is currently in a tree.Whether this [State] object is currently in a tree.
Stateがツリーに存在するかどうかを返してくれるわけですね。
以上、Flutter人口が増えて、もっと日本語での情報が増えればいいなと思っている今日この頃の赤ん坊でした。