A zone represents an environment that remains stable across asynchronous calls.
Code is always executed in the context of a zone, available as
Zone.current. The initial main
function runs in the context of the
default zone (Zone.ROOT). Code can be run in a different zone using either
runZoned, to create a new zone, or Zone.run to run code in the context of
an existing zone likely created using Zone.fork.
Developers can create a new zone that overrides some of the functionality of
an existing zone. For example, custom zones can replace of modify the
behavior of print
, timers, microtasks or how uncaught errors are handled.
The Zone class is not subclassable, but users can provide custom zones by
forking an existing zone (usually Zone.current) with a ZoneSpecification.
This is similar to creating a new class that extends the base Zone
class
and that overrides some methods, except without actually creating a new
class. Instead the overriding methods are provided as functions that
explicitly take the equivalent of their own class, the "super" class and the
this
object as parameters.
Asynchronous callbacks always run in the context of the zone where they were
scheduled. This is implemented using two steps:
1. the callback is first registered using one of registerCallback,
registerUnaryCallback, or registerBinaryCallback. This allows the zone
to record that a callback exists and potentially modify it (by returning a
different callback). The code doing the registration (e.g., Future.then
)
also remembers the current zone so that it can later run the callback in
that zone.
2. At a later point the registered callback is run in the remembered zone.
This is all handled internally by the platform code and most users don't need to worry about it. However, developers of new asynchronous operations, provided by the underlying system or through native extensions, must follow the protocol to be zone compatible.
For convenience, zones provide bindCallback (and the corresponding
bindUnaryCallback or bindBinaryCallback) to make it easier to respect the
zone contract: these functions first invoke the corresponding register
functions and then wrap the returned function so that it runs in the current
zone when it is later asynchronously invoked.
Constants
Static Properties
Properties
- errorZone → Zone
-
The error zone is the one that is responsible for dealing with uncaught errors.
read-only - hashCode → int
-
Get a hash code for this object.
read-only, inherited - parent → Zone
-
The parent zone of the this zone.
read-only - runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited
Operators
-
operator ==(
other) → bool -
The equality operator.
inherited -
operator [](
Object key) → dynamic -
Retrieves the zone-value associated with
key
.
Methods
-
bindBinaryCallback(
dynamic action(argument1, argument2), { bool runGuarded: true }) → ZoneBinaryCallback -
Equivalent to:
-
bindCallback(
dynamic action(), { bool runGuarded: true }) → ZoneCallback -
Equivalent to:
-
bindUnaryCallback(
dynamic action(argument), { bool runGuarded: true }) → ZoneUnaryCallback -
Equivalent to:
-
createPeriodicTimer(
Duration period, void callback(Timer timer)) → Timer -
Creates a periodic Timer where the callback is executed in this zone.
-
createTimer(
Duration duration, void callback()) → Timer -
Creates a Timer where the callback is executed in this zone.
-
errorCallback(
Object error, StackTrace stackTrace) → AsyncError -
Intercepts errors when added programatically to a
Future
orStream
. -
fork(
{ZoneSpecification specification, Map zoneValues }) → Zone -
Creates a new zone as a child of
this
. -
handleUncaughtError(
error, StackTrace stackTrace) → dynamic -
Handles uncaught asynchronous errors.
-
inSameErrorZone(
Zone otherZone) → bool -
Returns true if
this
andotherZone
are in the same error zone. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed.
inherited -
print(
String line) → void -
Prints the given
line
. -
registerBinaryCallback(
dynamic callback(arg1, arg2)) → ZoneBinaryCallback -
Registers the given callback in this zone.
-
registerCallback(
dynamic callback()) → ZoneCallback -
Registers the given callback in this zone.
-
registerUnaryCallback(
dynamic callback(arg)) → ZoneUnaryCallback -
Registers the given callback in this zone.
-
run(
dynamic action()) → dynamic -
Executes
action
in this zone. -
runBinary(
dynamic action(argument1, argument2), argument1, argument2) → dynamic -
Executes the given
action
withargument1
andargument2
in this zone. -
runBinaryGuarded(
dynamic action(argument1, argument2), argument1, argument2) → dynamic -
Executes the given
action
withargument1
andargument2
in this zone and catches synchronous errors. -
runGuarded(
dynamic action()) → dynamic -
Executes the given
action
in this zone and catches synchronous errors. -
runUnary(
dynamic action(argument), argument) → dynamic -
Executes the given
action
withargument
in this zone. -
runUnaryGuarded(
dynamic action(argument), argument) → dynamic -
Executes the given
action
withargument
in this zone and catches synchronous errors. -
scheduleMicrotask(
void action()) → void -
Runs
action
asynchronously in this zone. -
toString(
) → String -
Returns a string representation of this object.
inherited