Dart Developer Library
Essential tools for debugging and profiling Dart applications
🛠️ What is Dart Developer Library?
The Developer library provides debugging, profiling, and inspection tools to help you understand and optimize your Dart applications during development and testing.
import 'dart:developer';
void main() {
debugger(); // Pause execution for debugging
log('Application started', name: 'MyApp');
}
Debug Console:
[MyApp] Application started
Developer Tools
Logging
Advanced logging with categories
log('Debug message',
name: 'MyApp',
level: Level.INFO);
Debugging
Breakpoints and inspection
debugger(when: condition);
inspect(myObject);
Profiling
Performance measurement
Timeline.startSync('operation');
// code here
Timeline.finishSync();
Metrics
Custom performance metrics
var counter = Counter('requests');
counter.value++;
🔹 Logging and Debugging
Use structured logging for better debugging:
import 'dart:developer';
void main() {
// Basic logging
log('Application starting...');
// Logging with categories and levels
log('User login attempt',
name: 'Auth',
level: Level.INFO);
log('Database connection failed',
name: 'Database',
level: Level.SEVERE,
error: 'Connection timeout');
// Conditional debugging
var isDebugMode = true;
debugger(when: isDebugMode);
// Inspect objects
var user = {'name': 'John', 'age': 30};
inspect(user);
// Log with additional data
log('Processing order',
name: 'OrderService',
level: Level.INFO,
time: DateTime.now(),
sequenceNumber: 1001);
}
Debug Console Output:
[INFO] Application starting...
[Auth] User login attempt
[Database] SEVERE: Database connection failed
[OrderService] Processing order (seq: 1001)
🔹 Performance Profiling
Measure and analyze application performance:
import 'dart:developer';
void main() async {
// Time a synchronous operation
Timeline.startSync('data-processing');
processData();
Timeline.finishSync();
// Time an asynchronous operation
Timeline.timeSync('database-query', () {
return queryDatabase();
});
// Manual timing with flow events
var flowId = Flow.begin();
Timeline.startSync('step-1');
await step1();
Timeline.finishSync();
Timeline.startSync('step-2');
await step2();
Timeline.finishSync();
Flow.end(flowId);
// Custom performance counters
var requestCounter = Counter('http_requests', 'Number of HTTP requests');
requestCounter.value = 150;
var responseTime = Gauge('response_time_ms', 'Average response time');
responseTime.value = 45.2;
}
void processData() {
// Simulate data processing
for (int i = 0; i < 1000000; i++) {
// Processing logic
}
}
Future<void> queryDatabase() async {
await Future.delayed(Duration(milliseconds: 100));
}
Future<void> step1() async {
await Future.delayed(Duration(milliseconds: 50));
}
Future<void> step2() async {
await Future.delayed(Duration(milliseconds: 30));
}
Performance Timeline:
data-processing: 15.2ms
database-query: 102.1ms
step-1: 51.3ms
step-2: 31.7ms
🔹 Service Extensions
Create custom debugging tools and extensions:
import 'dart:developer';
import 'dart:convert';
void main() {
// Register a custom service extension
registerExtension('ext.myapp.getStats', (method, parameters) async {
var stats = {
'uptime': DateTime.now().millisecondsSinceEpoch,
'memory_usage': '45MB',
'active_users': 127,
'requests_per_second': 23.5
};
return ServiceExtensionResponse.result(jsonEncode(stats));
});
// Register a service to toggle debug mode
var debugMode = false;
registerExtension('ext.myapp.toggleDebug', (method, parameters) async {
debugMode = !debugMode;
log('Debug mode: ${debugMode ? 'ON' : 'OFF'}', name: 'DebugToggle');
return ServiceExtensionResponse.result(jsonEncode({
'debug_mode': debugMode
}));
});
// Register a service to clear cache
registerExtension('ext.myapp.clearCache', (method, parameters) async {
// Simulate cache clearing
await Future.delayed(Duration(milliseconds: 100));
log('Cache cleared successfully', name: 'CacheManager');
return ServiceExtensionResponse.result(jsonEncode({
'status': 'success',
'message': 'Cache cleared'
}));
});
log('Service extensions registered', name: 'ServiceManager');
}
Available Extensions:
ext.myapp.getStats - Get application statistics
ext.myapp.toggleDebug - Toggle debug mode
ext.myapp.clearCache - Clear application cache