检查运行的应用程序是否处于调试模式

Kev*_*ter 31 flutter

我有一个简短的问题.当应用程序处于调试模式时,我正在寻找一种在Flutter中执行代码的方法.这可能在Flutter?我似乎无法在文档中的任何地方找到它.

像这样的东西

If(app.inDebugMode) {
   print("Print only in debug mode");
}
Run Code Online (Sandbox Code Playgroud)

Rém*_*let 92

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

摇树(也就是编译器删除未使用的代码)取决于变量是常量。

问题在于,断言我们的isInReleaseMode布尔值不是常数。因此,在交付我们的应用程序时,将同时包含开发和发布代码。

另一方面,kReleaseMode 一个常数。因此,编译器能够正确删除未使用的代码,并且我们可以放心地执行以下操作:

if (kReleaseMode) {

} else {
  // Will be tree-shaked on release builds.
}
Run Code Online (Sandbox Code Playgroud)

  • 还有`kDebugMode` (17认同)
  • 还要注意一点,为避免污染您的班级而使用未知的import,例如,将“ import'package:package:flutter / foundation.dart”作为Foundation;然后可以执行Foundation。kReleaseMode``` (4认同)
  • 正如 @Oliver 指出的,避免符号污染的另一个技巧是使用 [`show`](https://dart.dev/guides/language/language-tour#libraries-and-visibility) 命令仅导入符号)你想使用,即 `import 'package:flutter/foundation.dart' show kDebugMode;` (4认同)
  • 小部件也会发生 Tree Shaking 吗?因此,如果我使用visible: kDebugMode 制作了一个可见性小部件,那么编译器会在发布版本时删除该小部件吗? (3认同)
  • 到目前为止情况如何,这应该是公认的答案! (2认同)

Had*_*ard 49

这个小片段应该做你需要的

bool get isInDebugMode {
  bool inDebugMode = false;
  assert(inDebugMode = true);
  return inDebugMode;
}
Run Code Online (Sandbox Code Playgroud)

如果没有,您可以配置IDE以启动不同main.dart的调试模式,您可以在其中设置布尔值.

  • 这在发布中被称为。使用常量 kDebugMode (2认同)

cre*_*not 38

kDebugMode

您现在可以使用kDebugMode常量.

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 {

}
Run Code Online (Sandbox Code Playgroud)

这比!kReleaseMode它更可取,因为它还检查配置文件模式,即kDebugMode意味着不在发布模式不在配置文件模式

kReleaseMode

如果您只想检查发布模式而不是配置文件模式,则可以使用kReleaseMode

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 {

}
Run Code Online (Sandbox Code Playgroud)

kProfileMode

如果您只想检查配置文件模式而不是发布模式,则可以kProfileMode改用:

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 {

}
Run Code Online (Sandbox Code Playgroud)


Nee*_*raj 24

不挑剔,但基础包包含一个kDebugMode常量;所以 :

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

if(Foundation.kDebugMode) {
   print("App in debug mode");
}
Run Code Online (Sandbox Code Playgroud)


Kal*_*ani 20

这是一个简单的解决方案:

import 'package:flutter/foundation.dart';
Run Code Online (Sandbox Code Playgroud)

那么你可以使用kReleaseMode

if(kReleaseMode){ // is Release Mode ??
    print('release mode');
} else {
    print('debug mode');
}
Run Code Online (Sandbox Code Playgroud)


rmt*_*zie 17

最简单的方法是使用assert它,因为它只在调试模式下运行.

以下是Flutter的Navigator源代码示例:

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;
}());
Run Code Online (Sandbox Code Playgroud)

特别注意在()调用结束时 - assert只能在布尔值上操作,所以只传入一个函数不起作用.

  • “特别注意”是我的 IDE 绊倒的部分。太感谢了! (2认同)
  • 当您编写`(){....}`时,您是在定义函数,而不是调用它。添加`()实际上会调用该函数。 (2认同)

Tra*_*der 6

我相信最新的方法是:

const bool prod = const bool.fromEnvironment('dart.vm.product');
Run Code Online (Sandbox Code Playgroud)

源文件


Par*_*iya 5

这是找出应用程序以哪种模式运行的两个步骤

  1. 添加以下导入以获取

    import 'package:flutter/foundation.dart' as Foundation;
    
    Run Code Online (Sandbox Code Playgroud)
  2. kReleaseMode检查应用程序正在运行的模式

    if(Foundation.kReleaseMode){ 
      print('app release mode');
    } else {
      print('App debug mode');
    }
    
    Run Code Online (Sandbox Code Playgroud)


Ana*_*nan 5

只需导入这个

import 'package:flutter/foundation.dart' 


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

or

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

or

String bulid = kProfileMode ? "Profile" : "";
Run Code Online (Sandbox Code Playgroud)

或者试试这个

if (kDebugMode) {
   print("Debug");
} else if (kReleaseMode) {
    print("Release"); 
} else if (kProfileMode) {
  print("Profile"); 
}
Run Code Online (Sandbox Code Playgroud)