0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Flutter アニメーションディレイでありがち Failed assertionを乗り越える

0
Posted at

こんにちは、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人口が増えて、もっと日本語での情報が増えればいいなと思っている今日この頃の赤ん坊でした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?