18

There is some confusion when to use print and debugPrint, so there are some statements which may be false, and which should be clearified:

  1. When using the direct print method then it will bring a lot of a trash into production, will not it?

  2. When using the debugPrint method then it will print only while developing, or it will print also in production?

  3. When I generate a release file (apk), will not it delete all the print calls to optimize the app and reduce the release file size?

rozerro
  • 5,787
  • 9
  • 46
  • 94

1 Answers1

28
  1. and 3. If you use the command flutter logs you will see the output of the print function in all applications you have installed in the phone/emulator. This means that even if the app is in release mode, it will still be printed in the terminal.
  2. debugPrint generally is used to avoid the printing limit of each OS, if default debugPrintThrottled callback is used. It will also print in production, but you can customize it to work only in dev mode:
void main() {

    if (kReleaseMode) {
      debugPrint = (String? message, {int? wrapWidth}) {};
    }
}

With this when the debugPrint statements in your code called in production, it won’t be printed to the console since you gave this function an empty callback.

Tayan
  • 1,707
  • 13
  • 18
Simon Sot
  • 2,748
  • 2
  • 10
  • 23
  • 8
    The example above for overriding debugPrint in release mode is a good solution. Another way to do it with slightly less code is to import flutter/foundation.dart and check kReleaseMode. For example: ```if (kReleaseMode) { debugPrint = (String? message, { int? wrapWidth }) {}; }``` – most200 Oct 04 '21 at 04:04