Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switching between debugging and production mode

Tags:

flutter

dart

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?

like image 999
mrs.tat Avatar asked Mar 19 '19 12:03

mrs.tat


People also ask

What is the difference between debug and Release mode in Visual Studio?

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.

What is difference between development mode and production mode?

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.

What is production mode?

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.


2 Answers

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

like image 69
Rémi Rousselet Avatar answered Oct 14 '22 05:10

Rémi Rousselet


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

like image 38
Günter Zöchbauer Avatar answered Oct 14 '22 05:10

Günter Zöchbauer