initialize static method

void initialize({
  1. ErrorCallback? errorCallback,
})

Sets up error interception for all three error zones.

Safe to call multiple times — subsequent calls are no-ops. Only active in debug mode (kDebugMode). In release builds, this method does nothing.

Implementation

static void initialize({ErrorCallback? errorCallback}) {
  if (_initialized) return;
  _initialized = true;
  onError = errorCallback;

  // ── Zone 1: Flutter widget/framework errors ───────────────────────────
  _originalFlutterOnError = FlutterError.onError;
  FlutterError.onError = _handleFlutterError;

  // ── Zone 2: Unhandled async errors (Flutter 3.1+) ─────────────────────
  final originalPlatformError = ui.PlatformDispatcher.instance.onError;
  ui.PlatformDispatcher.instance.onError = (Object error, StackTrace stack) {
    _emitCrashEvent(
      error,
      stack,
      zone: 'platform_dispatcher',
      summary: error.toString(),
    );
    // Let the original handler run too (if any)
    return originalPlatformError?.call(error, stack) ?? true;
  };
}