Skip to main content

Create a Flutter project in Sentry and copy its DSN into .env

For sentry

The app reports its errors to the Sentry project whose DSN is in SENTRY_DSN of .env. While the key is empty, or has a value that is no DSN of Sentry, the app starts without Sentry and reports nothing to it, and a debug build prints why.

A DSN only lets an app send events to its project, and it goes into the app, where anyone can read it, so it is no secret.

.env is one file, not tied to a flavor. The environment of each event is the flavor of the build, such as dev or prod, and a debug build sends its events too. So keep two projects: one for development, whose DSN goes into your local .env, and one for the release, whose DSN goes into the values that a build for a store gets.

1. Create the projects. In Sentry, create a Flutter project for the release, and a second one for development.

2. Give each DSN a rate limit. In the settings of each project, give the key of the DSN a rate limit, so that one app that fails in a loop does not use up the quota of the organization. The free plan of Sentry has a monthly quota of errors, which the pricing of Sentry tells.

3. Put the DSN into .env. Copy the DSN of the project for development into .env at the root of the app:

.env
SENTRY_DSN=https://...

.env stays out of git. On another machine, copy .env.example to .env and fill it in again.

4. Give a build for a store the DSN of the release. A build for a store gets the DSN of the project of the release, such as through the values of the environment prod of your CI.

5. Start the app again. The flutter tool reads .env when a command starts, so stop flutter run and start it again with the values:

flutter run --dart-define-from-file=.env

Run and Debug of VS Code, and the run configuration of the app in Android Studio, pass the argument for you.

Check. A debug build no longer prints a line that starts with initSentryCrashReporter() failed, so the app works without it, such as one that says that SENTRY_DSN has no value in this build. An error that the app reports, such as one that the code reports with recordError() of CrashReporter, reaches the project for development, with the environment dev.