postCatchedException<T> static method

void postCatchedException<T>(
  1. T callback(), {
  2. FlutterExceptionHandler? handler,
  3. String? filterRegExp,
  4. bool debugUpload = false,
})

异常上报

Implementation

static void postCatchedException<T>(
  T callback(), {
  FlutterExceptionHandler? handler, //异常捕捉,用于自定义打印异常
  String? filterRegExp, //异常上报过滤正则,针对message
  bool debugUpload = false,
}) {
  bool _isDebug = false;
  assert(_isDebug = true);
  Isolate.current.addErrorListener(new RawReceivePort((dynamic pair) {
    var isolateError = pair as List<dynamic>;
    var _error = isolateError.first;
    var _stackTrace = isolateError.last;
    Zone.current.handleUncaughtError(_error, _stackTrace);
  }).sendPort);
  // This creates a [Zone] that contains the Flutter application and stablishes
  // an error handler that captures errors and reports them.
  //
  // Using a zone makes sure that as many errors as possible are captured,
  // including those thrown from [Timer]s, microtasks, I/O, and those forwarded
  // from the `FlutterError` handler.
  //
  // More about zones:
  //
  // - https://api.dartlang.org/stable/1.24.2/dart-async/Zone-class.html
  // - https://www.dartlang.org/articles/libraries/zones
  runZonedGuarded<Future<Null>>(() async {
    callback();
  }, (error, stackTrace) {
    _filterAndUploadException(
      debugUpload,
      _isDebug,
      handler,
      filterRegExp,
      FlutterErrorDetails(exception: error, stack: stackTrace),
    );
  });
  // This captures errors reported by the Flutter framework.
  FlutterError.onError = (details) {
    Zone.current.handleUncaughtError(details.exception, details.stack!);
  };
}