As a fast way of debugging app while developing is writing a statement like:
print($data)
is there is a way to stop printing when switching to production mode so it will not affect the performance of the app?
a boolean as a switch for example?
By default, Debug includes debug information in the compiled files (allowing easy debugging) while Release usually has optimizations enabled. As far as conditional compilation goes, they each define different symbols that can be checked in your program, but they are language-specific macros.
Production mode minifies your code and better represents the performance your app will have on end users' devices. Development mode includes useful warnings and gives you access to tools that make development and debugging easier.
In the Production mode, you cannot copy your files or database to the Production environment. This protects you from possibly destroying your live application by overwriting your Production files and databases. You copy databases and files up to the Production environment only until you first launch your application.
You can use debugPrint
instead of print
for dev only logging
debugPrint(data)
debugPrint
implementation can be made to change between environment. For instance in your main you can do:
void main() {
bool isInRelease = true;
assert(() { isInRelease = false; return true; }());
if (isInRelease) {
debugPrint = (String? message, { int? wrapWidth }) {};
}
}
This will replace the implementation of debugPrint
with something that does nothing in release
https://docs.flutter.io/flutter/foundation/debugPrint.html would allow this.
The docs don't tell if it prints in production mode, but you could run different main()
that assigns a no-op function to debugPrint
.
Another way would be to use How do I build different versions of my Flutter app for qa/dev/prod? or the assert trick Does Flutter remove debug-mode code when compiling for release? to override debugPrint
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