How to check flutter application is running in debug?

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

    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.

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

    I believe the latest way to do this is:

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

    src

    0 讨论(0)
  • 2020-12-08 13:09

    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.

    0 讨论(0)
  • 2020-12-08 13:12

    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.

    0 讨论(0)
  • 2020-12-08 13:14

    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');
    }
    
    0 讨论(0)
提交回复
热议问题