Movatterモバイル変換


[0]ホーム

URL:


menu
  1. Dart
  2. dart:async
  3. Future<T>
  4. Future.delayed factory constructor
Future.delayed
description

Future<T>.delayed constructor

Future<T>.delayed(
  1. Durationduration, [
  2. FutureOr<T>computation()?
])

Creates a future that runs its computation after a delay.

Thecomputation will be executed after the givenduration has passed,and the future is completed with the result of the computation.

Ifcomputation returns a future,the future returned by this constructor will complete with the value orerror of that future.

If the duration is 0 or less,it completes no sooner than in the next event-loop iteration,after all microtasks have run.

Ifcomputation is omitted,it will be treated as ifcomputation was() => null,and the future will eventually complete with thenull value.In that case,T must be nullable.

If callingcomputation throws, the created future will complete with theerror.

See alsoCompleter for a way to create and complete a future at alater time that isn't necessarily after a known fixed duration.

Example:

Future.delayed(const Duration(seconds: 1), () {  print('One second has passed.'); // Prints after 1 second.});

Implementation

factory Future.delayed(Duration duration, [FutureOr<T> computation()?]) {  if (computation == null && !typeAcceptsNull<T>()) {    throw ArgumentError.value(      null,      "computation",      "The type parameter is not nullable",    );  }  _Future<T> result = _Future<T>();  Timer(duration, () {    if (computation == null) {      result._complete(null as T);    } else {      FutureOr<T> computationResult;      try {        computationResult = computation();      } catch (e, s) {        _completeWithErrorCallback(result, e, s);        return;      }      result._complete(computationResult);    }  });  return result;}
  1. Dart
  2. dart:async
  3. Future<T>
  4. Future.delayed factory constructor
Future class

[8]ページ先頭

©2009-2025 Movatter.jp