runApp function

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

Add an Error Handler right at the start.

Implementation

void runApp(
  m.Widget app, {
  FlutterExceptionHandler? onError,
  bool? runZoneGuard,
}) {
  // Assign an error handler (Will be replaced after)
  c.AppErrorHandler.set(handler: _RunAppErrorHandler(onError).handler);

  // Instantiate the app's error handler.
  final handler = c.AppErrorHandler();

  // 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);
  }
}