【问题标题】:How to differentiate between debug and release mode in flutter in code? [duplicate]如何区分代码中颤振中的调试和发布模式? [复制]
【发布时间】:2019-08-31 13:42:10
【问题描述】:

我只想在发布模式下拥有一些功能,而不是在调试模式下。越过它的时间越长,仅仅在开发过程中评论它不是一个好主意。因为在发布版本时总是有可能忘记它。

【问题讨论】:

标签: dart flutter


【解决方案1】:

通过导入flutter/foundation.dart,此检查可使用顶级常量:

kReleaseMode

这比断言更好,因为它适用于摇树。

【讨论】:

  • 摇树是什么意思?
  • 编译器将删除未使用的代码。因此,如果您正在执行if (kReleaseMode) {} else {},那么编译器就会知道else 永远不会被执行,因此将其删除。
【解决方案2】:

这对我来说效果很好。 声明如下函数;

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

现在你可以像这样使用它:

if(isInDebugMode) {
    print('Debug');
} else {
    print('Release');
}

Source of information

================================================ ========================== 你也可以使用@Rémi Rousselet 给出的solution

首先导入包:

import 'package:flutter/foundation.dart';

并像这样使用kReleaseMode

if(kReleaseMode) { // is in Release Mode ?
    print('Release');
} else {
    print('Debug');
}

【讨论】:

    猜你喜欢
    • 2019-10-22
    • 2011-07-17
    • 2020-01-28
    • 2021-06-01
    • 2011-03-19
    • 2021-04-13
    • 2020-07-07
    相关资源
    最近更新 更多