我正在寻找一种在应用程序处于调试模式时在Flutter中执行代码的方法。在颤振有可能吗?我在文件里找不到。
就像这样
If(app.inDebugMode) {
print("Print only in debug mode");
}
如何检查Flutter应用程序是运行在调试模式还是发布模式?
我正在寻找一种在应用程序处于调试模式时在Flutter中执行代码的方法。在颤振有可能吗?我在文件里找不到。
就像这样
If(app.inDebugMode) {
print("Print only in debug mode");
}
如何检查Flutter应用程序是运行在调试模式还是发布模式?
当前回答
我相信最新的做法是:
const bool prod = const bool.fromEnvironment('dart.vm.product');
src
其他回答
创建一个名为constants.dart的文件。把这些变量加进去:
const bool kReleaseMode = bool.fromEnvironment('dart.vm.product');
const bool kProfileMode = bool.fromEnvironment('dart.vm.profile');
const bool kDebugMode = !kReleaseMode && !kProfileMode;
printk(String string) {
if (kDebugMode) {
// ignore: avoid_print
print(string);
}
}
然后在任何其他文件中导入这个常量文件,并像这样使用它:
import 'package:package_name/constants.dart';
if(kDebugMode){
//Debug code
}else{
//Non-Debug code
}
printk("Debug Log");
我相信最新的做法是:
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');
}
k调试模式
现在可以使用kDebugMode常量了。
if (kDebugMode) {
// Code here will only be included in debug mode.
// As kDebugMode is a constant, the tree shaker
// will remove the code entirely from compiled code.
} else {
}
这比!kReleaseMode更可取,因为它也检查配置文件模式,即,kDebugMode表示不在发布模式和不在配置文件模式。
kReleaseMode
如果你只是想检查释放模式,而不是配置文件模式,你可以使用kReleaseMode代替:
if (kReleaseMode) {
// Code here will only be run in release mode.
// As kReleaseMode is a constant, the tree shaker
// will remove the code entirely from other builds.
} else {
}
kProfileMode
如果你只是想检查配置文件模式,而不是发布模式,你可以使用kProfileMode代替:
if (kProfileMode) {
// Code here will only be run in release mode.
// As kProfileMode is a constant, the tree shaker
// will remove the code entirely from other builds.
} else {
}
导入这个
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");
}