Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: when to use `print` and `debugPrint`?

Tags:

flutter

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?

like image 809
rozerro Avatar asked Sep 07 '25 07:09

rozerro


1 Answers

  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:
import "package:flutter/foundation.dart"; //allows to use kReleaseMode

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.

like image 97
Simon Sot Avatar answered Sep 10 '25 20:09

Simon Sot