Flutter - Sentry how to send event and stop sending in debug mode
Asked Answered
R

3

9

How to send specific information in Sentry ? There is Events in documentation but how to use them properly and where exactly to use them (EX: Send user email with the error) ?

Sentry provided this source code but where exactly I have to use it? :

 Sentry.configureScope(
      (scope) => scope.user = SentryUser(id: '1234', email: '[email protected]'),
    );

And also how to stop sending reports in debug mode ?

Robet answered 1/11, 2021 at 12:33 Comment(0)
A
19

Just to add an easy way to disable reporting in debugMode for anyone in the future:

Pass an empty string to the dsn in SentryFlutter.init if it's not in Release or Profile.

kDebugMode constant is available when importing Foundation

  await SentryFlutter.init(
    (options) {
      options.dsn = kDebugMode ? '' : sentryUrl;
    },
    appRunner: () => runApp(MyApp()),
  );
Antacid answered 14/11, 2021 at 17:12 Comment(2)
When I do this, exceptions aren't printed to the console anymore. You can add options.debug = kDebugMode; to get them back.Embrey
Easiest and most elegant solutionCharron
M
4

I simply only initialise Sentry if the app is in the release mode.

if (kReleaseMode) {
  // Only enable Sentry in release builds.
  await SentryFlutter.init(
    (options) {
      options.dsn = 'https://<your DSN>';
    },
    appRunner: () => runApp(const MyApp()),
  );
} else {
  runApp(const MyApp());
}
Maddi answered 27/2, 2022 at 16:37 Comment(0)
H
1

@Rock setting the user depends on your own business logic, the only important thing is to call Sentry.configureScope(...) after initializing the SDK, rather than that, any place would work.

For not sending events on debug mode, there are many ways to do it, you could simply not initialize the SDK when its debug mode, or you could filter events on debug mode https://docs.sentry.io/platforms/flutter/configuration/filtering/

Huertas answered 2/11, 2021 at 12:46 Comment(2)
Hi @Manoel thanks for response, I put the Sentry.configureScope(...) in the main after initializing sentry but unfortunately it did not work. In the sentry panel it still shows that the user is unknownRobet
I have noticed that I am trying to send user data before user initialization, so I put this method after user initialization and it worked. Thanks a lotRobet

© 2022 - 2024 — McMap. All rights reserved.