scheduleTask<T> method

Future<T> scheduleTask<T>(
  1. TaskCallback<T> task,
  2. Priority priority, {
  3. String? debugLabel,
  4. Flow? flow,
})

Schedules the given task with the given priority.

If task returns a future, the future returned by scheduleTask will complete after the former future has been scheduled to completion. Otherwise, the returned future for scheduleTask will complete with the same value returned by task after it has been scheduled.

The debugLabel and flow are used to report the task to the Timeline, for use when profiling.

Processing model

Tasks will be executed between frames, in priority order, excluding tasks that are skipped by the current schedulingStrategy. Tasks should be short (as in, up to a millisecond), so as to not cause the regular frame callbacks to get delayed.

If an animation is running, including, for instance, a ProgressIndicator indicating that there are pending tasks, then tasks with a priority below Priority.animation won't run (at least, not with the defaultSchedulingStrategy; this can be configured using schedulingStrategy).

Implementation

Future<T> scheduleTask<T>(
  TaskCallback<T> task,
  Priority priority, {
  String? debugLabel,
  Flow? flow,
}) {
  final bool isFirstTask = _taskQueue.isEmpty;
  final _TaskEntry<T> entry = _TaskEntry<T>(
    task,
    priority.value,
    debugLabel,
    flow,
  );
  _taskQueue.add(entry);
  if (isFirstTask && !locked) {
    _ensureEventLoopCallback();
  }
  return entry.completer.future;
}