---
title: "File I/O"
description: "Learn more about the Sentry file I/O integration for the Dart SDK."
url: https://docs.sentry.io/platforms/dart/integrations/file/
---

# File I/O | Sentry for Dart

References to "transactions" on this page apply to the default transaction mode. In stream mode, this integration creates service spans instead, and Sentry sends them as they finish. See [Streamed Spans](https://docs.sentry.io/platforms/dart/tracing/streamed-spans.md) for more information.

File I/O operations are fundamental for reading from and writing to files within an application. The `sentry_file` integration provides [File](https://api.dart.dev/stable/2.18.5/dart-io/File-class.html) support for Sentry thus providing insight into tracing of file operations.

## [Behavior](https://docs.sentry.io/platforms/dart/integrations/file.md#behavior)

* The created spans attach to the active span. In transaction mode, that's the transaction bound to the scope; in stream mode, it's the span started with `Sentry.startSpan()`. If no span is active, the File I/O span won't be sent to Sentry.
* The File I/O integration is only available for the `dart:io:File` class, not for the `dart:html:File` class.
* The SDK sets the span `operation` to `file.copy`, `file.write`, `file.delete`, `file.open`, `file.read` or `file.rename`, and `description` to `filename` (for example, `file.txt`).
* The span finishes once the operation has been executed. The span `status` is then set to `SpanStatus.ok` if successful, or `SpanStatus.internalError` if there was an error.
* When the operation throws an `Exception`, Sentry's SDK associates it with the running span.

## [Prerequisites](https://docs.sentry.io/platforms/dart/integrations/file.md#prerequisites)

1. The Sentry [Dart](https://docs.sentry.io/platforms/dart.md#configure) SDK is initialized.
2. Tracing is set up. Learn more [here](https://docs.sentry.io/platforms/dart/tracing.md).

## [Install](https://docs.sentry.io/platforms/dart/integrations/file.md#install)

To use the `SentryFile` wrapper, add the `sentry_file` dependency.

```yml
dependencies:
  sentry: ^9.25.0
  sentry_file: ^9.25.0
```

## [Configure](https://docs.sentry.io/platforms/dart/integrations/file.md#configure)

Call the Sentry extension method to wrap the file:

```dart
final sentryFile = file.sentryTrace();
```

## [Verify](https://docs.sentry.io/platforms/dart/integrations/file.md#verify)

### [1. Execute the Code](https://docs.sentry.io/platforms/dart/integrations/file.md#1-execute-the-code)

**Transaction Mode (Default)**

```dart
import 'package:sentry/sentry.dart';
import 'package:sentry_file/sentry_file.dart';
import 'dart:io';

Future<void> main() async {
  await Sentry.init(
    (options) {
      options.dsn = 'https://example@sentry.io/example';
      // To set a uniform sample rate
      options.tracesSampleRate = 1.0;
    },
    appRunner: runApp, // Init your App.
  );
}

Future<void> runApp() async {
  final file = File('my_file.txt');
  // Call the Sentry extension method to wrap up the File
  final sentryFile = file.sentryTrace();

  // Start a transaction if there's no active transaction
  final transaction = Sentry.startTransaction(
    'MyFileExample',
    'file',
    bindToScope: true,
  );

  // create the File
  await sentryFile.create();
  // Write some content
  await sentryFile.writeAsString('Hello World');
  // Read the content
  final text = await sentryFile.readAsString();

  print(text);

  // Delete the file
  await sentryFile.delete();

  // Finish the transaction
  await transaction.finish(status: SpanStatus.ok());

  await Sentry.close();
}
```

**Stream Mode**

```dart
import 'package:sentry/sentry.dart';
import 'package:sentry_file/sentry_file.dart';
import 'dart:io';

Future<void> main() async {
  await Sentry.init(
    (options) {
      options.dsn = 'https://example@sentry.io/example';
      // To set a uniform sample rate
      options.tracesSampleRate = 1.0;
      // Enables stream mode
      options.traceLifecycle = SentryTraceLifecycle.stream;
    },
    appRunner: runApp, // Init your App.
  );
}

Future<void> runApp() async {
  final file = File('my_file.txt');
  // Call the Sentry extension method to wrap up the File
  final sentryFile = file.sentryTrace();

  // Start a root span — file I/O spans attach to the active span
  await Sentry.startSpan('MyFileExample', (span) async {
    // create the File
    await sentryFile.create();
    // Write some content
    await sentryFile.writeAsString('Hello World');
    // Read the content
    final text = await sentryFile.readAsString();

    print(text);

    // Delete the file
    await sentryFile.delete();
  }, parentSpan: null);

  await Sentry.close();
}
```

### [2. View the Recorded Transaction on Sentry.io](https://docs.sentry.io/platforms/dart/integrations/file.md#2-view-the-recorded-transaction-on-sentryio)

To view the recorded transaction, log into [sentry.io](https://sentry.io) and open your project. Clicking **Performance** will open a page with transactions, where you can select the just recorded transaction with the name `MyFileExample`.
