How can I check if a Flutter application is running in debug?
Asked Answered
C

13

300

I'm looking for a way to execute code in Flutter when the app is in Debug mode. Is that possible in Flutter? I can't seem to find it anywhere in the documentation.

Something like this

If(app.inDebugMode) {
   print("Print only in debug mode");
}

How can I check if the Flutter application is running in debug or release mode?

Counterstatement answered 7/4, 2018 at 11:45 Comment(2)
Check Flutter mode from DartIcterus
I tried assert(() { print("Debug mode"); return true; }); but that just gives me an error that can't compile. The "Profile" they are talking about later in the post doesn't make much sense to me. Can you please explain how to use it ?Counterstatement
E
61

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.

Echolocation answered 7/4, 2018 at 13:19 Comment(3)
The "Note in particular" was the part that my IDE was tripping over. Thank you so much!Counterstatement
When you write () { .... } you're defining the function, but not calling it. Adding the () actually calls the function.Echolocation
This seems a lot more semantically correct than checking the kdebugmode const.Sneaker
Q
603

In later versions, you can 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.
}
Quinquennial answered 10/4, 2019 at 12:39 Comment(7)
Just a small side note as well, to avoid pollution your class with unknowns import like following import 'package:flutter/foundation.dart' as Foundation; then you can do Foundation. kReleaseModeBeaufert
How is this so far down, this should be the accepted answer!Transfix
Also there is kDebugModeFiacre
Will tree shaking also occur with widgets? So if I did a Visibility widget with visible: kDebugMode will that widget be removed by the compiler for release builds?Brochure
Can we also know if the .apk is signed with signingConfigs.debug with the same constant?Activate
upvoted - but jus a note - @AlexanderSkvortsov mentioned kDebugMode (als upvoted). kDebugMode has the advantage of cleaning out your debug code while profiling (kReleaseMode is afaik false when profiling so !kReleaseMod is inferior to kDebugMode)Ritchey
Another trick to avoid symbol pollution as @Oliver noted is to use the show command to import just the symbol(s) you want to use, i.e. import 'package:flutter/foundation.dart' show kDebugMode;Law
A
105

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');
}
Athirst answered 10/4, 2019 at 8:45 Comment(1)
if you need code for release - use kReleaseMode... the else bit is either debug or profiling... use kDebugMode for debug and kProfileMode as neededRitchey
A
72

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 preferable 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 {

}
Ancy answered 21/6, 2020 at 18:24 Comment(0)
Y
69

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


This little snippet 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.

Yaekoyael answered 7/4, 2018 at 15:52 Comment(2)
I put it as static in an Application class so I can write Application.isInDebugMode where I need it.Dehumanize
this is called in release. use constants kDebugModeRitchey
E
61

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.

Echolocation answered 7/4, 2018 at 13:19 Comment(3)
The "Note in particular" was the part that my IDE was tripping over. Thank you so much!Counterstatement
When you write () { .... } you're defining the function, but not calling it. Adding the () actually calls the function.Echolocation
This seems a lot more semantically correct than checking the kdebugmode const.Sneaker
H
29

Not to be picky, but the foundation package includes a kDebugMode constant.

So:

import 'package:flutter/foundation.dart' as Foundation;

if(Foundation.kDebugMode) {
   print("App in debug mode");
}
Highsounding answered 18/2, 2020 at 22:55 Comment(0)
B
11

I believe the latest way to do this is:

const bool prod = const bool.fromEnvironment('dart.vm.product');

src

Bloodstained answered 13/9, 2020 at 21:18 Comment(3)
Re "the latest way": Can you add some version/date information?Pyx
As of Dart 2.9.0, this is not recommended, see do_not_use_environment: "Using values derived from the environment at compile-time, creates hidden global state and makes applications hard to understand and maintain."Originative
It strikes me that the assert() is the best way. No need to remember arbitrary const names. assert is documented to trigger in debug mode. Now, there may be fuzzy issues between debug and production but the OP was asking for how to detect if in debug mode.Sneaker
G
5

Just import this

import 'package:flutter/foundation.dart' 


String bulid = kReleaseMode ? "Release" : "";

or

String bulid = kDebugMode ? "Debug" : "";

or

String bulid = kProfileMode ? "Profile" : "";

Or try this

if (kDebugMode) {
   print("Debug");
} else if (kReleaseMode) {
    print("Release"); 
} else if (kProfileMode) {
  print("Profile"); 
}
Goatsucker answered 8/9, 2022 at 4:39 Comment(0)
B
3

I've created this useful class, based on other answers and inspired on Android usage. If anything changes on "Foundation" package, it would not be necessary to change the entire application, it would be necessary to change only this class.

import 'package:flutter/foundation.dart' as Foundation;

abstract class Build {

    static const bool isDebugMode = Foundation.kDebugMode;

    static const bool isReleaseMode = Foundation.kReleaseMode;

    static const bool isWeb = Foundation.kIsWeb;

    static const bool isProfileMode = Foundation.kProfileMode;
}
Bile answered 8/9, 2021 at 14:5 Comment(0)
S
2

Make a file named constants.dart. Add these variables in it:

const bool kReleaseMode = bool.fromEnvironment('dart.vm.product');
const bool kProfileMode = bool.fromEnvironment('dart.vm.profile');
const bool kDebugMode = !kReleaseMode && !kProfileMode;

printk(String string) {
  if (kDebugMode) {
    // ignore: avoid_print
    print(string);
  }
}

Then import this constant file in any other file and use it like this:

    import 'package:package_name/constants.dart';

    if(kDebugMode){
        //Debug code
    }else{
        //Non-Debug code
    }

    printk("Debug Log");
Syllogism answered 31/1, 2022 at 12:14 Comment(0)
E
2

you can use flutter's foundation library. You can get debug, profile and release

import 'package:flutter/foundation.dart';

then

if (kDebugMode) {
  // debug code
}

if (kProfileMode) {
 // profile code
}

if (kReleaseMode) {
    // release code
}
Envision answered 18/7, 2023 at 11:21 Comment(0)
T
0

Extracted from Dart Documentation:

When exactly do assertions work? That depends on the tools and framework you’re using:

  • Flutter enables assertions in debug mode.
  • Development-only tools such as dartdevc typically enable assertions by default.
  • Some tools, such as dart and dart2js, support assertions through a command-line flag: --enable-asserts.

In production code, assertions are ignored, and the arguments to assert aren’t evaluated.

Tetrapterous answered 20/3, 2020 at 20:15 Comment(1)
How does that answer the question? It is "How can I check if the Flutter application is running in debug or release mode?" Can you summarise in your own words?Pyx
S
0

In flutter you can use “foundations” package to check is the app debug or release by using the following constants:

  • kReleaseMode
  • kProfileMode
  • kDebugMode

**

Here is an example:

**

import 'package:flutter/foundation.dart';

modeFinder() {
  String currentMode;

  if (kDebugMode) {
       currentMode = "Debug mode"
  } else if (kReleaseMode) {
       currentMode = "Release mode"
  } else {
       currentMode = "Profile mode"
  }

  print(string);
}
Slotter answered 28/12, 2023 at 12:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.