Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check flutter application is running in debug?

Update You can now use kDebugMode:

if (kDebugMode)
  doSomething();

While asserts can technically be used to manually create an "is debug mode" variable, you should avoid that.

Instead, use the constant kReleaseMode from package:flutter/foundation.dart


The difference is all about tree shaking

Tree shaking (aka the compiler removing unused code) depends on variables being constants.

The issue is, with asserts our isInReleaseMode boolean is not a constant. So when shipping our app, both the dev and release code are included.

On the other hand, kReleaseMode is a constant. Therefore the compiler is correctly able to remove unused code, and we can safely do:

if (kReleaseMode) {

} else {
  // Will be tree-shaked on release builds.
}

Here is a simple solution to this:

import 'package:flutter/foundation.dart';

then you can use kReleaseMode like

if(kReleaseMode){ // is Release Mode ??
    print('release mode');
} else {
    print('debug mode');
}

Update

Please use Remi's answer with kReleaseMode and kDebugMode or Dart compilation won't be able to tree-shake your code


this little snippets should do what you need

bool get isInDebugMode {
  bool inDebugMode = false;
  assert(inDebugMode = true);
  return inDebugMode;
}

if not you can configure your IDE to launch a different main.dart in debug mode where you can set a boolean.



While this works, using constants kReleaseMode or kDebugMode is preferable. See Rémi's answer below for a full explanation, which should probably be the accepted question.


The easiest way is to use assert as it only runs in debug mode.

Here's an example from Flutter's Navigator source code:

assert(() {
  if (navigator == null && !nullOk) {
    throw new FlutterError(
      'Navigator operation requested with a context that does not include a Navigator.\n'
      'The context used to push or pop routes from the Navigator must be that of a '
      'widget that is a descendant of a Navigator widget.'
    );
  }
  return true;
}());

Note in particular the () at the end of the call - assert can only operate on a boolean, so just passing in a function doesn't work.


kDebugMode

You can now use the kDebugMode constant.

if (kDebugMode) {
  // Code here will only be included in debug mode.
  // As kDebugMode is a constant, the tree shaker
  // will remove the code entirely from compiled code.
} else {

}

This is preferrable over !kReleaseMode as it also checks for profile mode, i.e. kDebugMode means not in release mode and not in profile mode.

kReleaseMode

If you just want to check for release mode and not for profile mode, you can use kReleaseMode instead:

if (kReleaseMode) {
  // Code here will only be run in release mode.
  // As kReleaseMode is a constant, the tree shaker
  // will remove the code entirely from other builds.
} else {

}

kProfileMode

If you just want to check for profile mode and not for release mode, you can use kProfileMode instead:

if (kProfileMode) {
  // Code here will only be run in release mode.
  // As kProfileMode is a constant, the tree shaker
  // will remove the code entirely from other builds.
} else {

}