我正在寻找一种在应用程序处于调试模式时在Flutter中执行代码的方法。在颤振有可能吗?我在文件里找不到。
就像这样
If(app.inDebugMode) {
print("Print only in debug mode");
}
如何检查Flutter应用程序是运行在调试模式还是发布模式?
我正在寻找一种在应用程序处于调试模式时在Flutter中执行代码的方法。在颤振有可能吗?我在文件里找不到。
就像这样
If(app.inDebugMode) {
print("Print only in debug mode");
}
如何检查Flutter应用程序是运行在调试模式还是发布模式?
当前回答
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 {
}
其他回答
我创建了这个有用的类,基于其他答案和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;
}
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';
然后你可以使用kReleaseMode like
if(kReleaseMode){ // Is Release Mode??
print('release mode');
} else {
print('debug mode');
}
虽然这是可行的,但使用常量kReleaseMode或kDebugMode更可取。请参阅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只能操作布尔值,因此仅仅传入一个函数是行不通的。
创建一个名为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");