How to exclude debug code

£可爱£侵袭症+ 提交于 2019-12-02 01:30:11
Günter Zöchbauer

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.

You can also take a look at this question How to achieve precompiler directive like functionality

I put @GünterZöchbauer "assert trick" inside the factory constructor:

class _ProductionPlug implements DebugClass{
  const _ProductionPlug();
  noSuchMethod(_) {} //do nothing
}

class DebugClass{
  static final DebugClass _plug = const _ProductionPlug();
  log(msg){print(msg);}
  DebugClass._(){}
  factory DebugClass(){
    DebugClass instance;
    assert((){
    instance = new DebugClass._();
    return true;
        });
    return instance != null ?  instance :  _plug;
  }
}
void main() {
  print("hello");
  new DebugClass()
    ..log("debugging");
}

This way nothing sticks out.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!