dart:developer library
Interact with developer tools such as the debugger and inspector.
This is a specialized library intended for interacting with the Dart runtimeprogrammatically for debugging and inspection. Sample uses include advanceddebugging, and creating developer tools.
This library has platform specific implementations for Dart weband Dart Native (VM). A specific platform may not support all operations.
The functionality provided by this library is generally only availableto Dart code run in development mode, e.g.,dart run, and not in productionmode, e.g., the output ofdart compile exe.
Debugging
Thedebugger function can be used to stop the program as if a breakpointwas hit. The breakpoint will be placed right after the call todebugger.This functionality can be useful for triggering breakpoints based on logicin the code.
Example:
var counter = 0;final someInterestingValue = 1000;while (true) { if (counter == someInterestingValue) { // Trigger a breakpoint in the debugger. debugger(); } counter++;}When executed withdart run --observe, and opened in DevTools, thedebugger will be stopped withcounter at the value1000.
Inspection
Developer tools, such as Dart DevTools, connected to the runtime systemmay allow for inspecting execution timing in a "timeline" view.The static methods ofTimeline can add extra information and timing eventsto this view.
Example:
void main() { Timeline.timeSync('Calculation loop', () { for (var i = 30; i < 50; i++) { Timeline.timeSync('fib($i)', () { fibonacci(i); }); } });}int fibonacci(int n) => (n < 2) ? n : fibonacci(n - 2) + fibonacci(n - 1);When executed withdart run --observe, and opened in DevTools,the Performance tab will display a timeline containing the annotationspassed totimeSync.
Developer tools
A developer tool, like the debugger built into thedart command,may access information about the running applicationexposed by the runtime system,using theService andServiceProtocolInfo classes.
Classes
- Flow
- A class to represent Flow events.
- NativeRuntime
- Functionality available on the native runtime.
- Service
- Access information about the service protocol and control the web serverthat provides access to the services provided by the Dart VM fordebugging and inspecting Dart programs.
- ServiceExtensionResponse
- A response to a service protocol extension RPC.
- ServiceProtocolInfo
- Service protocol is the protocol that a client like the Observatorycould use to access the services provided by the Dart VM fordebugging and inspecting Dart programs. This class encapsulates theversion number and Uri for accessing this service.
- Timeline
- Add to the timeline.
- TimelineTask
- An asynchronous task on the timeline. An asynchronous task can have many(nested) synchronous operations. Synchronous operations can live longer thanthe current isolate event. To pass aTimelineTask to another isolate,you must first callpass to get the task id and then construct a newTimelineTask in the other isolate.
- UserTag
- A UserTag can be used to group samples in theDevTools CPU profiler.
Properties
- extensionStreamHasListener→bool
- Whether the "Extension" stream currently has at least one listener.no setter
- reachabilityBarrier→int
- Current reachability barrier state.no setter
Functions
- addHttpClientProfilingData(
Map< String,dynamic> requestProfile)→ void - Records the data associated with an HTTP request for profiling purposes.
- debugger(
{boolwhen =true,String?message})→bool - If
whenis true, stop the program as if a breakpoint were hit at thefollowing statement. - getCurrentTag(
)→UserTag - Returns the currentUserTag for the isolate.
- getHttpClientProfilingData(
)→List< Map< String,dynamic> > - Returns the data added throughaddHttpClientProfilingData.
- inspect(
Object?object)→Object? - Send a reference to
objectto any attached debuggers. - log(
Stringmessage, {DateTime?time,int?sequenceNumber,intlevel =0,Stringname ='',Zone?zone,Object?error,StackTrace?stackTrace})→ void - Emit a log event, which can can viewed using the DevToolsLogging view.
- postEvent(
StringeventKind,MapeventData, {Stringstream ='Extension'})→ void - Post an event of
eventKindwith payload ofeventDatato the "Extension"event stream. - registerExtension(
Stringmethod,ServiceExtensionHandlerhandler)→ void - Register aServiceExtensionHandler that will be invoked in this isolatefor
method.NOTE: Service protocol extensions must be registeredin each isolate.
Typedefs
- ServiceExtensionHandler=Future<
ServiceExtensionResponse> Function(Stringmethod,Map<String,String> parameters) - A service protocol extension handler. Registered withregisterExtension.
- TimelineAsyncFunction=Future Function()
- TimelineSyncFunction<
T>= T Function() - A typedef for the function argument toTimeline.timeSync.