Rewrite Frames

Learn how and why to rewrite stack trace frames in Sentry's flutter SDK.

There are times when you may want to rewrite stack trace frames before sending them to Sentry. For example, if you're publishing your Flutter application on the web and want to set a url_prefix because it's not deployed at the root of your URL. In this case, the absolute path of your stack frames also needs to include the same prefix so that the source maps can be found for deobfuscation.

To set the url_prefix, use the Sentry Dart Plugin. Below is an example of how to update the stack frame's absolute path to include the prefix using the beforeSendhook.

Copied
options.beforeSend = (event, hint) async {
  final exceptions = event.exceptions?.map((exception) {
    final stackTrace = exception.stackTrace;
    if (stackTrace != null) {
      final frames = stackTrace.frames.map((frame) {
        const baseUrl = 'https://example.com/';
        final modifiedAbsPath = frame.absPath?.replaceFirst(
          baseUrl,
          '${baseUrl}my_prefix/',
        );
        return frame.copyWith(absPath: modifiedAbsPath);
      }).toList();
      return exception.copyWith(
        stackTrace: SentryStackTrace(frames: frames),
      );
    }
    return exception;
  }).toList();
  return event.copyWith(exceptions: exceptions ?? []);
};
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").