Task<T> class
final
A Task is an object representing some operation that will complete in the future.
Tasks are executed asynchronously and cooperatively.
Cooperative multitasking is a concurrency model where tasks voluntarily yield control (using await).
The result of a task execution is the result of computing the value of the task action. It can be either a value or an exception.
The task itself is an object of Future that wraps the result of the computation.
The main difference between the task and the Future is as follows:
The task does not begin executing the computation immediately after it is created.
The task supports delayed start. Or it may never even be started.
After the computation is completed, the task captures the result of the computation.
In case of completion with an exception, the task does not propagate this exception to the unhandled exception handler immediately.
This unobserved exception is stored in the relevant task object instance until it the task is aware that an exception has been observed.
If the task isn not aware that an exception was observed, this exception will be propagated in the task finalizer (Finalizer).
If the finalizer is not executed by runtime (due to Dart SDK limitations), the exception will remain unobserved.
For this reason, due to the limited functionality of the finalizer, it is recommended to always observe task exceptions (detecting, catching, handling).
Exceptions in task can be observed in one of the following ways:
await tasktask.futureTask.waitAll()task.asStream()(inherited from Future)task.catchError()(inherited from Future)task.then()(inherited from Future)task.timeout()(inherited from Future)task.whenComplete()(inherited from Future)
It all comes down to the fact that when accessing the _future field of a task, an instance of the Future object is created and at that moment its life cycle begins.
- Implemented types
-
- Future<
T>
- Future<
- Available extensions
Constructors
Properties
- exception → ErrorResult?
-
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- id → int
-
Returns a unique integer identifier for the task.
final
- isCanceled → bool
-
Returns
trueif the task is in the TaskState.canceled state. ; otherwise, returnsfalse.no setter - isCompleted → bool
-
Returns
trueif the task is in the TaskState.completed state. ; otherwise, returnsfalse.no setter - isFailed → bool
-
Returns
trueif the task is in the TaskState.failed state. ; otherwise, returnsfalse.no setter - isRunning → bool
-
Returns
trueif the task was started and it is not completed; otherwise, returnsfalse.no setter - isStarted → bool
-
Returns
trueif the task was started for execution; otherwise, returnsfalse.no setter - isTerminated → bool
-
Returns
trueif the task was terminated; otherwise, returnsfalse.no setter - name → String?
-
Returns the task name.
final
- result → T
-
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- state → TaskState
-
Returns task state (TaskState).
no setter
-
toJS
→ JSPromise<
T> -
Available on Future<
A JSPromise that either resolves with the result of the completed Future or rejects with an object that contains its error.T> , provided by the FutureOfJSAnyToJSPromise extensionno setter -
toJS
→ JSPromise<
JSAny?> -
Available on Future<
A JSPromise that either resolves once this Future completes or rejects with an object that contains its error.void> , provided by the FutureOfVoidToJSPromise extensionno setter - zoneStats → ZoneStats?
-
no setter
Methods
-
asStream(
) → Stream< T> -
Creates a Stream containing the result of this future.
override
-
catchError(
Function onError, {bool test(Object error)?}) → Future< T> -
Handles errors emitted by this Future.
override
-
ignore(
) → void -
Available on Future<
Completely ignores this future and its result.T> , provided by the FutureExtensions extension -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onError<
E extends Object> (FutureOr< T> handleError(E error, StackTrace stackTrace), {bool test(E error)?}) → Future<T> -
Available on Future<
Handles errors on this future.T> , provided by the FutureExtensions extension -
start(
) → Future< void> - Starts execution of the task.
-
then<
R> (FutureOr< R> onValue(T value), {Function? onError}) → Future<R> -
Register callbacks to be called when this future completes.
override
-
timeout(
Duration timeLimit, {FutureOr< T> onTimeout()?}) → Future<T> -
Stop waiting for this future after
timeLimithas passed.override -
toString(
) → String -
A string representation of this object.
override
-
whenComplete(
FutureOr< void> action()) → Future<T> -
Registers a function to be called when this future completes.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
delay(
[int milliseconds = 0, CancellationToken? token]) → Task< void> - Creates a task that will complete after a time delay.
-
onExit(
FutureOr< void> handler(AnyTask task)) → void -
Assigns a
handlerfor the current task that will be executed after the task is terminated -
run<
T> (FutureOr< T> action(), {String? name}) → Task<T> -
Creates and starts a task with the specified
actioncallback andname. -
sleep(
[int milliseconds = 0, CancellationToken? token]) → Future< void> - Allows to switch the event loop context to execute other scheduled code in the microtask and timer queue.
-
waitAll<
T> (List< Task< tasks, {Progress<T> >({int count, int total})> ? progress}) → Future<void> -
Performs a wait operation for tasks to complete.
If all tasks are completed successfully, then this method will also complete successfully. -
whenAll<
T> (List< Task< tasks, {Progress<T> >({int count, int total})> ? progress}) → Task<List< T> > -
Performs a wait operation for tasks to complete.
When all tasks have completed successfully, returns a new task with the results of the awaited tasks. -
whenAny<
T> (List< Task< tasks, {Progress<T> >({int count, int total})> ? progress}) → Task<Task< T> > -
Performs a wait operation for tasks to complete.
As soon as one of the tasks is completed, it will be immediately returned as the result of this method. -
whenEach<
T> (List< Task< tasks, {Progress<T> >({int count, int total})> ? progress}) → Stream<Task< T> > -
Returns a Stream to which each Task in the
taskslist will be added, in the order in which they were completed.