registerErrorHandlers function

void registerErrorHandlers({
  1. void onFlutterError(
    1. FlutterErrorDetails details
    )?,
  2. ErrorWidgetBuilder? errorWidgetBuilder,
  3. bool presentFlutterErrors = true,
  4. bool handlePlatformErrors = true,
  5. bool onPlatformError(
    1. Object error,
    2. StackTrace stack
    )?,
})

Installs global error handlers for Flutter framework errors and async platform errors.

Pass onFlutterError / onPlatformError to forward errors to your crash reporter. Set presentFlutterErrors to false to suppress the red error screen. Optionally override the error widget builder with errorWidgetBuilder.

Implementation

void registerErrorHandlers({
  void Function(FlutterErrorDetails details)? onFlutterError,
  ErrorWidgetBuilder? errorWidgetBuilder,
  bool presentFlutterErrors = true,
  bool handlePlatformErrors = true,
  bool Function(Object error, StackTrace stack)? onPlatformError,
}) {
  FlutterError.onError = (FlutterErrorDetails details) {
    if (presentFlutterErrors) {
      FlutterError.presentError(details);
    }
    if (onFlutterError != null) {
      onFlutterError(details);
    } else {
      debugPrint(details.toString());
    }
  };

  if (handlePlatformErrors) {
    PlatformDispatcher.instance.onError = (Object error, StackTrace stack) {
      if (onPlatformError != null) {
        return onPlatformError(error, stack);
      }
      debugPrint(error.toString());
      return true;
    };
  }

  if (errorWidgetBuilder != null) {
    ErrorWidget.builder = errorWidgetBuilder;
  }
}