我有一个简短的问题.当应用程序处于调试模式时,我正在寻找一种在Flutter中执行代码的方法.这可能在Flutter?我似乎无法在文档中的任何地方找到它.
像这样的东西
If(app.inDebugMode) {
print("Print only in debug mode");
}
Run Code Online (Sandbox Code Playgroud)
Rém*_*let 92
While asserts technically works, you should not use them.
Instead, use the constant kReleaseMode from package:flutter/foundation.dart
The difference is all about tree shaking
摇树(也就是编译器删除未使用的代码)取决于变量是常量。
问题在于,断言我们的isInReleaseMode布尔值不是常数。因此,在交付我们的应用程序时,将同时包含开发和发布代码。
另一方面,kReleaseMode 是一个常数。因此,编译器能够正确删除未使用的代码,并且我们可以放心地执行以下操作:
if (kReleaseMode) {
} else {
// Will be tree-shaked on release builds.
}
Run Code Online (Sandbox Code Playgroud)
Had*_*ard 49
这个小片段应该做你需要的
bool get isInDebugMode {
bool inDebugMode = false;
assert(inDebugMode = true);
return inDebugMode;
}
Run Code Online (Sandbox Code Playgroud)
如果没有,您可以配置IDE以启动不同main.dart的调试模式,您可以在其中设置布尔值.
cre*_*not 38
kDebugMode您现在可以使用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 {
}
Run Code Online (Sandbox Code Playgroud)
这比!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 {
}
Run Code Online (Sandbox Code Playgroud)
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 {
}
Run Code Online (Sandbox Code Playgroud)
Nee*_*raj 24
不挑剔,但基础包包含一个kDebugMode常量;所以 :
import 'package:flutter/foundation.dart' as Foundation;
if(Foundation.kDebugMode) {
print("App in debug mode");
}
Run Code Online (Sandbox Code Playgroud)
Kal*_*ani 20
这是一个简单的解决方案:
import 'package:flutter/foundation.dart';
Run Code Online (Sandbox Code Playgroud)
那么你可以使用kReleaseMode像
if(kReleaseMode){ // is Release Mode ??
print('release mode');
} else {
print('debug mode');
}
Run Code Online (Sandbox Code Playgroud)
rmt*_*zie 17
最简单的方法是使用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;
}());
Run Code Online (Sandbox Code Playgroud)
特别注意在()调用结束时 - assert只能在布尔值上操作,所以只传入一个函数不起作用.
我相信最新的方法是:
const bool prod = const bool.fromEnvironment('dart.vm.product');
Run Code Online (Sandbox Code Playgroud)
这是找出应用程序以哪种模式运行的两个步骤
添加以下导入以获取
import 'package:flutter/foundation.dart' as Foundation;
Run Code Online (Sandbox Code Playgroud)
并kReleaseMode检查应用程序正在运行的模式
if(Foundation.kReleaseMode){
print('app release mode');
} else {
print('App debug mode');
}
Run Code Online (Sandbox Code Playgroud)
只需导入这个
import 'package:flutter/foundation.dart'
String bulid = kReleaseMode ? "Release" : "";
or
String bulid = kDebugMode ? "Debug" : "";
or
String bulid = kProfileMode ? "Profile" : "";
Run Code Online (Sandbox Code Playgroud)
或者试试这个
if (kDebugMode) {
print("Debug");
} else if (kReleaseMode) {
print("Release");
} else if (kProfileMode) {
print("Profile");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8189 次 |
| 最近记录: |