WillW
WillW

Reputation: 891

Difference between methods of determining debug or release in Dart

I am aware of two methods of determining whether my app is in running in debug mode:

const bool.fromEnvironment("dart.vm.product") returns true if release.

And this from the Sentry part of the Flutter docs:

bool get isInDebugMode {
  // Assume we're in production mode
  bool inDebugMode = false;

  // Assert expressions are only evaluated during development. They are ignored
  // in production. Therefore, this code will only turn `inDebugMode` to true
  // in our development environments!
  assert(inDebugMode = true);

  return inDebugMode;
}

Are those two always equivalent or are there situations where they would give different answers? Which should I use? The first method being compile time seems to favour it.

Upvotes: 6

Views: 3691

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 658037

In general they should be the same, but there can be differences.

const bool.fromEnvironment("dart.vm.product") depends on release build being performed. I haven't checked if profile build returns true or false

assert(inDebugMode = true); depends on asserts being enabled. asserts are enabled in debug mode by default and disabled in release builds by default but there should be a way to enable/disable asserts independently of release/debug mode, but I haven't found how. Perhaps it's not exposed in Flutter or it is not implemented in Dart yet.

I'd think bool.fromEnvironment() works better with tree-shaking because it can be used to create a const value.

Upvotes: 7

Related Questions