【问题标题】:How to check flutter application is running in debug?如何检查颤振应用程序是否在调试中运行?
【发布时间】:2018-09-17 07:44:11
【问题描述】:

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

类似的东西

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

如何检查flutter应用是在debug还是release模式下运行?

【问题讨论】:

  • 我试过 assert(() { print("Debug mode"); return true; });但这只是给我一个无法编译的错误。他们稍后在帖子中谈论的“个人资料”对我来说没有多大意义。你能解释一下如何使用它吗?

标签: flutter debugging dart flutter-run flutter-debug


【解决方案1】:

更新您现在可以使用kDebugMode

if (kDebugMode)
  doSomething();

虽然断言在技术上可用于手动创建“处于调试模式”的变量,但您应该避免这样做。

改为使用来自package:flutter/foundation.dart 的常量kReleaseMode


区别在于摇树

Tree Shaking(即编译器删除未使用的代码)取决于变量是否为常量。

问题是,断言我们的isInReleaseMode 布尔值不是常量。因此,在发布我们的应用程序时,开发和发布代码都包含在内。

另一方面,kReleaseMode 一个常数。因此编译器能够正确地删除未使用的代码,我们可以安全地这样做:

if (kReleaseMode) {

} else {
  // Will be tree-shaked on release builds.
}

【讨论】:

  • 只是一个小旁注,为了避免未知数的导入污染你的班级,比如关注import 'package:flutter/foundation.dart' as Foundation;,那么你可以做Foundation. kReleaseMode
  • 怎么这么低,这应该是公认的答案!
  • 还有kDebugMode
  • 小部件是否也会发生树抖动?所以,如果我做了一个可见的小部件可见:kDebugMode 编译器是否会删除该小部件以进行发布构建?
  • 我们还能知道.apk 是否使用signingConfigs.debug 以相同的常量签名?
【解决方案2】:

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

import 'package:flutter/foundation.dart';

那么你可以使用kReleaseModelike

if(kReleaseMode){ // is Release Mode ??
    print('release mode');
} else {
    print('debug mode');
}

【讨论】:

  • 如果您需要发布代码 - 使用 kReleaseMode...else 位是调试或分析...根据需要使用 kDebugMode 进行调试和 kProfileMode
【解决方案3】:

更新

请将 Remi 的答案与 kReleaseModekDebugMode 一起使用,否则 Dart 编译将无法对您的代码进行 tree-shaking


这个小sn-ps应该做你需要的

bool get isInDebugMode {
  bool inDebugMode = false;
  assert(inDebugMode = true);
  return inDebugMode;
}

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

【讨论】:

  • 我把它作为静态放在 Application 类中,这样我就可以在需要的地方写 Application.isInDebugMode
  • 非常优雅,在我的应用中实现了这个。
  • 这是在发布时调用的。使用常量 kDebugMode
【解决方案4】:

虽然这可行,但最好使用常量kReleaseModekDebugMode。请参阅下面的Rémi's answer 以获得完整的解释,这应该是公认的问题。


最简单的方法是使用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;
}());

特别注意调用结束时的() - assert 只能对布尔值进行操作,因此仅传入一个函数是行不通的。

【讨论】:

  • “特别注意”是我的 IDE 出错的部分。非常感谢!
  • 当您编写() { .... } 时,您是在定义函数,而不是调用它。添加()实际上调用了函数。
【解决方案5】:

kDebugMode

您现在可以使用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 {

}

这比!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 {

}

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 {

}

【讨论】:

    【解决方案6】:

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

    import 'package:flutter/foundation.dart' as Foundation;
    
    if(Foundation.kDebugMode) {
       print("App in debug mode");
    }
    

    【讨论】:

      【解决方案7】:

      我相信最新的方法是:

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

      src

      【讨论】:

        【解决方案8】:

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

        1. 添加以下导入以获取

          import 'package:flutter/foundation.dart' as Foundation;
          
        2. kReleaseMode检查应用程序运行的模式

          if(Foundation.kReleaseMode){ 
            print('app release mode');
          } else {
            print('App debug mode');
          }
          

        【讨论】:

          【解决方案9】:

          我创建了这个有用的类,基于其他答案并受到 Android 使用的启发。 如果“Foundation”包发生任何变化,则无需更改整个应用程序,只需更改此类即可。

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

          【讨论】:

            【解决方案10】:

            摘自 Dart Documentation:

            断言究竟什么时候起作用?这取决于工具和 您正在使用的框架:

            • Flutter 在 debug 模式下启用断言。
            • dartdevc 等开发专用工具通常默认启用断言。
            • 某些工具(例如 dart 和 dart2js)通过命令行标志支持断言:--enable-asserts。

            生产代码中,断言被忽略,而 断言不被评估。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-02-13
              • 2021-01-05
              • 2011-06-12
              • 1970-01-01
              • 2021-06-12
              • 2016-12-02
              • 1970-01-01
              • 2020-05-22
              相关资源
              最近更新 更多