如何排除调试代码 [英] How to exclude debug code

查看:117
本文介绍了如何排除调试代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有简单的记录器:

Let's say I have simple logger:

void main() {
  var logger = new MyLogger();
  logger.log("hello Dart");
}



我希望这个代码在dev模式我不想在我的生产代码。我希望它与dart2js编译器树摇动。是否有一些标准的方法?

I want this code to run in the dev mode (VM checked mode) but i don't want it in my production code. And i want it to be "tree shaked" away with the dart2js compiler. Is there some standard way?

推荐答案

您可以将代码嵌入 assert 。断言在生产代码中被忽略,并且我确信在 pub build release 模式下运行时,不会构建JS。

You could embed the code in an assert. Assertions are ignored in production code and I'm sure not built to JS when pub build is run in release mode.

class X {
  X() {
    print('x created');
  }

  void log(String m) {
    print(m);
  }
}

bool log(String m) {
  new X()..log(m);
      return true;
}

void main() {
  assert(() {
    new X()..log('in Assert');
    return true;
  });

  assert(() => log('in Assert')); // use a wrapper function
}

当您创建一个包装器方法返回<$

When you create a wrapper method that returns true than you don't have to do it explicit each time.

你也可以看看这个问题

这篇关于如何排除调试代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆