Chain class

A chain of stack traces.

A stack chain is a collection of one or more stack traces that collectively represent the path from main through nested function calls to a particular code location, usually where an error was thrown. Multiple stack traces are necessary when using asynchronous functions, since the program's stack is reset before each asynchronous callback is run.

Stack chains can be automatically tracked using Chain.capture. This sets up a new Zone in which the current stack chain is tracked and can be accessed using Chain.current. Any errors that would be top-leveled in the zone can be handled, along with their associated chains, with the onError callback. For example:

Chain.capture(() {
  // ...
}, onError: (error, stackChain) {
  print("Caught error $error\n"
        "$stackChain");
});
Implemented types

Constructors

Chain(Iterable<Trace> traces)
Returns a new Chain comprised of traces.
Chain.current([int level = 0])
Returns the current stack chain.
factory
Chain.forTrace(StackTrace trace)
Returns the stack chain associated with trace.
factory
Chain.parse(String chain)
Parses a string representation of a stack chain.
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
terse Chain
Returns a terser version of this chain.
no setter
traces List<Trace>
The stack traces that make up this chain.
final

Methods

foldFrames(bool predicate(Frame), {bool terse = false}) Chain
Returns a new Chain based on this chain where multiple stack frames matching predicate are folded together.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
override
toTrace() Trace
Converts this chain to a Trace.

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

capture<T>(T callback(), {void onError(Object error, Chain)?, bool when = true, bool errorZone = true, Map<Object?, Object?>? zoneValues}) → T
If when is true, runs callback in a Zone in which the current stack chain is tracked and automatically associated with (most) errors.
disable<T>(T callback(), {bool when = true}) → T
If when is true and this is called within a Chain.capture zone, runs callback in a Zone in which chain capturing is disabled.
track(Object? futureOrStream) → dynamic
Returns futureOrStream unmodified.