runApp function

void runApp(
  1. Widget app, {
  2. FlutterExceptionHandler? onError,
  3. bool? setErrorHandler,
  4. bool? runZoneGuard,
})

Add an Error Handler right at the start.

Implementation

void runApp(
  m.Widget app, {
  FlutterExceptionHandler? onError,
  bool? setErrorHandler,
  bool? runZoneGuard,
}) {
  // By default, set the Error Handler
  setErrorHandler ??= true;

  if (!setErrorHandler) {
    // No error handler, no Zone guard
    runZoneGuard = false;
  }

  // Instantiate the Fluttery error handler.
  // Records the current Error Handler
  final handler = c.AppErrorHandler();

  if (setErrorHandler) {
    // Assign an error handler (Will be replaced after)
    c.AppErrorHandler.set(handler: _RunAppErrorHandler(onError).handler);

    // Isolate is not available on the Web
    if (!kIsWeb) {
      //
      Isolate.current.addErrorListener(RawReceivePort((dynamic pair) {
        //
        if (pair is List<dynamic>) {
          final isolateError = pair;
          handler.isolateError(
            isolateError.first.toString(),
            StackTrace.fromString(isolateError.last.toString()),
          );
        }
      }).sendPort);
    }
  }

  // Don't call WidgetsFlutterBinding.ensureInitialized()
  // since the runApp() function itself calls it internally

  // Run in a new zone with an error handler
  if (runZoneGuard ?? true) {
    // Catch any errors attempting to execute runApp();
    runZonedGuarded(() {
      m.runApp(app);
    }, handler.runZonedError);
  } else {
    m.runApp(app);
  }
}