Reputation: 697
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?
Upvotes: 15
Views: 3766
Reputation: 277037
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
Upvotes: 25
Reputation: 657338
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
Upvotes: 4