init static method
Implementation
static Future<void> init({
required String projectKey,
String? apiKey,
String? endpoint,
String? fallbackEndpoint,
bool enableLogging = false,
bool offlineBuffer = true,
bool captureBreadcrumbs = true,
}) async {
if (_initialized || _initializing) {
return;
}
final effectiveKey = (projectKey.trim().isNotEmpty)
? projectKey.trim()
: (apiKey != null && apiKey.trim().isNotEmpty)
? apiKey.trim()
: null;
if (effectiveKey == null) {
throw ArgumentError('TraceX projectKey (or apiKey) cannot be empty.');
}
_initializing = true;
try {
_projectKey = effectiveKey;
_enableLogging = enableLogging;
_offlineBuffer = offlineBuffer;
_captureBreadcrumbs = captureBreadcrumbs;
final targetEndpoint = (endpoint != null && endpoint.trim().isNotEmpty)
? endpoint.trim()
: defaultEndpoint;
WidgetsFlutterBinding.ensureInitialized();
// -----------------------------------------
// Initialize Services with Target Endpoint & Fallback
// -----------------------------------------
await ServicesLocator.init(
endpoint: targetEndpoint,
fallbackEndpoint: fallbackEndpoint,
);
// -----------------------------------------
// Get Dependencies From GetIt
// -----------------------------------------
_sendCrashDetails = sl<SendCrashDetails>();
_getEnvironmentDetails = sl<GetEnvironmentDetails>();
_getBreadcrumbDetails = sl<GetBreadcrumbDetails>();
_crashOffline = sl<CrashOffline>();
// -----------------------------------------
// Rate Limiter
// -----------------------------------------
_rateLimiter = CrashRateLimiter();
// -----------------------------------------
// Crash Queue
// -----------------------------------------
_crashQueue = CrashQueue(
crashOffline: _crashOffline!,
offlineBuffer: _offlineBuffer,
);
// -----------------------------------------
// Start Offline Retry
// -----------------------------------------
_connectivityMonitor = ConnectivityMonitor();
_connectivityMonitor!.startListening(
onReconnected: () {
final sendBatch = sl<SendBatchCrashDetails>();
_crashQueue?.flushOfflineInBatches(sendBatch.execute);
},
);
// -----------------------------------------
// Initialize Environment
// -----------------------------------------
await _getEnvironmentDetails!.initialize();
// -----------------------------------------
// Flutter Error Handler (Microtask Queue)
// -----------------------------------------
_previousFlutterErrorHandler = FlutterError.onError;
FlutterError.onError = (FlutterErrorDetails details) {
recordFlutterError(details);
_previousFlutterErrorHandler?.call(details);
};
// -----------------------------------------
// Platform Error Handler (Microtask Queue)
// -----------------------------------------
_previousPlatformErrorHandler = PlatformDispatcher.instance.onError;
PlatformDispatcher.instance.onError = (Object error, StackTrace stack) {
scheduleMicrotask(() {
_captureException(error, stack);
});
return _previousPlatformErrorHandler?.call(error, stack) ?? false;
};
_initialized = true;
_log('TraceX: Initialized successfully with endpoint: $targetEndpoint');
} catch (e, stack) {
debugPrint('TraceX: Initialization failed: $e');
debugPrint('$stack');
// -----------------------------------------
// Cleanup
// -----------------------------------------
await ServicesLocator.reset();
_initialized = false;
_crashHistory.clear();
_crashQueue?.dispose();
_crashQueue = null;
_rateLimiter = null;
_sendCrashDetails = null;
_getEnvironmentDetails = null;
_getBreadcrumbDetails = null;
_crashOffline = null;
_connectivityMonitor?.stopListening();
_connectivityMonitor = null;
// Restore previous handlers
FlutterError.onError = _previousFlutterErrorHandler;
PlatformDispatcher.instance.onError = _previousPlatformErrorHandler;
rethrow;
} finally {
_initializing = false;
}
}