How to check flutter application is running in debug?

后端 未结 9 1149
孤独总比滥情好
孤独总比滥情好 2020-12-08 12:25

I have a short question. 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 t

9条回答
  •  醉话见心
    2020-12-08 13:03

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

提交回复
热议问题