我正在寻找一种在应用程序处于调试模式时在Flutter中执行代码的方法。在颤振有可能吗?我在文件里找不到。

就像这样

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

如何检查Flutter应用程序是运行在调试模式还是发布模式?


当前回答

这里有一个简单的解决方案:

import 'package:flutter/foundation.dart';

然后你可以使用kReleaseMode like

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

其他回答

我创建了这个有用的类,基于其他答案和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;

}

请使用Remi的答案与kReleaseMode和kDebugMode或Dart编译将无法树摇你的代码。


这个小片段应该做你需要的:

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

如果不是,您可以配置IDE来启动不同的主程序。dart在调试模式,你可以设置一个布尔值。

我相信最新的做法是:

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

src

这里有一个简单的解决方案:

import 'package:flutter/foundation.dart';

然后你可以使用kReleaseMode like

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

导入这个

import 'package:flutter/foundation.dart' 


String bulid = kReleaseMode ? "Release" : "";

or

String bulid = kDebugMode ? "Debug" : "";

or

String bulid = kProfileMode ? "Profile" : "";

或者试试这个

if (kDebugMode) {
   print("Debug");
} else if (kReleaseMode) {
    print("Release"); 
} else if (kProfileMode) {
  print("Profile"); 
}