I want to have some functionality only in release mode, and not in debug. It's longer to get past it and just commenting it during development is not a good idea. As there is always a probability of forgetting about it when making release builds.
The easiest way is to use assert as it only runs in debug mode. Here's an example from Flutter's Navigator source code: assert(() { if (navigator == null && ! nullOk) { throw new FlutterError( 'Navigator operation requested with a context that does not include a Navigator.
Debug Mode: In debug mode the application will be slow. Release Mode: In release mode the application will be faster. Debug Mode: In the debug mode code, which is under the debug, symbols will be executed. Release Mode: In release mode code, which is under the debug, symbols will not be executed.
Use debug mode during development, when you want to use hot reload. Use profile mode when you want to analyze performance. Use release mode when you are ready to release your app.
By importing flutter/foundation.dart
, a top level constant is available for this check:
kReleaseMode
This is better than asserts, because it works with tree shaking.
This worked well for me. Declare a function like following;
bool get isInDebugMode {
bool inDebugMode = false;
assert(inDebugMode = true);
return inDebugMode;
}
Now you can use it like:
if(isInDebugMode) {
print('Debug');
} else {
print('Release');
}
Source of information
======================================================================== You can also use solution given by @Rémi Rousselet:
First import the package:
import 'package:flutter/foundation.dart';
and use kReleaseMode
like this:
if(kReleaseMode) { // is in Release Mode ?
print('Release');
} else {
print('Debug');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With