ensureInitialized static method

void ensureInitialized()

Idempotent init. Wires Flutter framework + platform-dispatcher error handlers so anything that throws is captured. Safe to call multiple times.

Implementation

static void ensureInitialized() {
  if (_instance != null) return;
  final t = TalkerFlutter.init(
    settings: TalkerSettings(
      // Cap the in-memory buffer so a noisy session doesn't eat
      // a tester's RAM. 500 lines covers a typical 30-min run
      // with breadcrumbs + a few exceptions.
      maxHistoryItems: 500,
      useHistory: true,
      useConsoleLogs: kDebugMode, // only mirror to console in dev
    ),
  );

  // Capture framework errors (build / layout / paint) — these would
  // otherwise just print the red error screen.
  final prevOnError = FlutterError.onError;
  FlutterError.onError = (FlutterErrorDetails details) {
    t.handle(details.exception, details.stack, 'FlutterError');
    prevOnError?.call(details);
  };

  // Capture errors from outside the Flutter framework (e.g. async
  // futures that reject without a Zone error handler). Returning
  // true tells the platform we handled it; we still log + return
  // false so the platform default (which might also log) still runs.
  final prevPlatformOnError = PlatformDispatcher.instance.onError;
  PlatformDispatcher.instance.onError = (error, stack) {
    t.handle(error, stack, 'PlatformDispatcher');
    return prevPlatformOnError?.call(error, stack) ?? false;
  };

  _instance = t;
  t.info('TPKTalker initialised — debug-log capture is live.');
}