小编典典

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

flutter

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

像这样

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

阅读 429

收藏
2020-08-13

共1个答案

小编典典


虽然这可行,但最好使用常量kReleaseModekDebugMode。有关完整说明,请参见下面的Rémi答案,这可能是公认的问题。


最简单的方法是使用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只能对布尔值进行操作,因此仅传入函数是行不通的。

2020-08-13