How to check flutter application is running in debug?

后端 未结 9 1094
孤独总比滥情好
孤独总比滥情好 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:16

    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");
    }
    
    0 讨论(0)
  • 2020-12-08 13:19

    While asserts technically works, you should not use them.

    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.
    }
    
    0 讨论(0)
  • 2020-12-08 13:24

    These are the two steps to find out in which mode the application runs

    1. Add the following imports for getting

      import 'package:flutter/foundation.dart' as Foundation;
      
    2. And kReleaseMode check which mode the application is running

      if(Foundation.kReleaseMode){ 
        print('app release mode');
      } else {
        print('App debug mode');
      }
      
    0 讨论(0)
提交回复
热议问题