How to check flutter application is running in debug?
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
Tree shaking (aka the compiler removing unused code) depends on variables being constants.
The issue is, with asserts our isInReleaseMode
boolean is not a constant. So when shipping our app, both the dev and release code are included.
On the other hand, kReleaseMode
is a constant. Therefore the compiler is correctly able to remove unused code, and we can safely do:
if (kReleaseMode) {
} else {
// Will be tree-shaked on release builds.
}
Here is a simple solution to this:
import 'package:flutter/foundation.dart';
then you can use kReleaseMode
like
if(kReleaseMode){ // is Release Mode ??
print('release mode');
} else {
print('debug mode');
}
this little snippets should do what you need
bool get isInDebugMode {
bool inDebugMode = false;
assert(inDebugMode = true);
return inDebugMode;
}
if not you can configure your IDE to launch a different main.dart
in debug mode where you can set a boolean.