captureCrashes static method

void captureCrashes()

Routes FlutterError.onError, PlatformDispatcher's onError, and isolate errors through e. Invoked by configure by default.

Errors seen previously by e/w/f (caught, logged, rethrown) are suppressed here via an Expando mark, so try {...} catch (err) { Log.e( ..., error: err); rethrow; } does not double-report.

Implementation

static void captureCrashes() {
  FlutterError.onError = (details) {
    if (_isReported(details.exception)) return;
    e(
      details.exceptionAsString(),
      error: details.exception,
      stackTrace: details.stack,
      fields: {
        if (details.library != null) 'library': details.library,
        if (details.context != null) 'context': details.context.toString(),
      },
    );
  };
  PlatformDispatcher.instance.onError = (error, stack) {
    if (_isReported(error)) return true;
    e('uncaught async', error: error, stackTrace: stack);
    return true;
  };
  Isolate.current.addErrorListener(
    RawReceivePort((dynamic pair) {
      final list = pair as List;
      e(
        'isolate error: ${list[0]}',
        stackTrace: StackTrace.fromString('${list[1]}'),
      );
    }).sendPort,
  );
}