2
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 1 year has passed since last update.

Flutter 次のイベントループまで一定時間実行を遅らせる

Posted at

ビルド後の処理でwidgetをレンダリングしてからアクセスするにはTimerで関数を呼び出してもいいのですがawaitを入れたい場合はいくつかの方法があり、

Future(() { /* FutureのAPIかasync/awaitを使う */ });
// mainを抜けた時点で実行されるのがmicrotask、useEffect内でasync/awaitを使用したい時など
Future.microtask(() { /* 処理内容 */ });
// 遅延処理
Future.delayed(Duration.zero, () { /* 処理内容 */ });

Future/Future.microtask/Future.delayedを利用するか、

// riverpod更新時にNavigator.of(context).pushで処理が衝突する現象を回避
WidgetsBinding.instance.addPostFrameCallback((_) { /* 処理内容 */ });
// import package:flutter/scheduler.dart; が必要
// WidgetsBindingと同様の処理
SchedulerBinding.instance.addPostFrameCallback((_) { /* 処理内容 */ });
// import 'dart:async';
scheduleMicrotask(() { /* 処理内容 */ });

WidgetsBinding/SchedulerBinding.instance.addPostFrameCallback/scheduleMicrotaskなどがあります。

どれを利用しても上手く動いてくれますが、よく目につくのはFuture.delayedです。

実行までのcontextwidgetのビルドを保証して次のアクセスを円滑にしてくれます。Flutter内部の問題を処理しながら動いてくれるのは後者の方ですが、見やすさで前者を利用される方が多いような気がします。

それぞれの動きを検証

import 'dart:async';

void main() async{
  print('1');
  await Future((){
    print('2');
    Future(()=>print("3"));
    scheduleMicrotask(() => print('4'));
    Future.microtask(()=>print("5"));
    new Future.delayed(new Duration(seconds:1),
                       () => print('6'));
    new Future(() => print('7'));
    new Future(() => print('8'));
    print('9');
  });
  scheduleMicrotask(() => print('10'));
  print('11');
}

// 1 2 9 11 4 5 10 3 7 8 6

Future.delayedでループしている動き

import 'dart:async';

  Future<void> main() async {
   int _count = 0;
   // 3秒ごとにカウントして行き、3回目に終了
   while(true) {
     await Future.delayed(Duration(seconds: 3));
     _count++;
     print("$_count回");
     if (_count == 3) {
      break;
    }
   }
 }
2
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
2
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?