installEarlyErrorHandlers function

void installEarlyErrorHandlers()

Installs synchronous, dependency-free error handlers that BUFFER caught errors into a bounded module-level list until the reporter attaches.

Call this as an explicit main() line, pre-runApp, right after WidgetsFlutterBinding.ensureInitialized() — never fold it into the gate/host/bootstrap. A widget's initState and the async bootstrap both run AFTER runApp, so folding would install these handlers too late to capture pre-mount / pre-attach errors (e.g. a FlutterError thrown while painting the splash frame, or an error before Crashlytics attaches inside dreamicBootstrap()).

When appInitErrorHandling later runs it FLUSHES (and clears) this buffer to the attached reporter on every one of its code paths, so nothing caught in the pre-attach window is lost.

Idempotent: a second call (e.g. a gate-retry re-run of any main()-shaped code) re-installs the same overwrite-idempotent FlutterError.onError / PlatformDispatcher.onError handlers and does not duplicate state.

Implementation

void installEarlyErrorHandlers() {
  FlutterError.onError = (FlutterErrorDetails details) {
    debugPrint('Early Flutter Error: ${details.exceptionAsString()}');
    debugPrint('Stack trace:\n${details.stack}');
    _bufferEarlyError(details.exception, details.stack);
  };

  PlatformDispatcher.instance.onError = (Object error, StackTrace stackTrace) {
    debugPrint('Early Platform Error: $error');
    debugPrint('Stack trace:\n$stackTrace');
    _bufferEarlyError(error, stackTrace);
    return true;
  };
}