'Avoid `print` calls in production code. (Documentation)

I started seeing this warning in all my print statements.

print('Foo'); // Warning: Avoid `print` calls in production code. 


Solution 1:[1]

It is because of the flutter_lints package which is implicitly added to new projects created after Flutter 2.3.0.

You can use any of the following solutions.

  1. To remove the warning in that single line:

    // ignore: avoid_print
    print('Hello World');
    
  2. To remove the warning in that file

    // ignore_for_file: avoid_print
    print('Hello World');
    
  3. To remove the warning from the whole project.

    Open analysis_options.yaml file and add this linter rule:

    include: package:flutter_lints/flutter.yaml
    
    linter:
      rules:
        avoid_print: false
    

Solution 2:[2]

You should use debugPrint(); instead

Solution 3:[3]

Developers shouldn't suppress such analyzer warnings as well as not use print() for apps they are building. print() will print logs in release builds as well, which is something that shouldn't happen as some developers might log sensitive information.

The suggested by other people debugPrint() is just a wrapper for print() to avoid losing some logs on Android in case the print() function called too often or the output there is too big.

What should be used instead is log() function available in dart.developer, which allows to also attach an error object, severity level, name, etc. of logged even and won't print anything in release mode, so no information will leak.

Here is more information about proper logging approach, which also describes log() function: https://docs.flutter.dev/testing/code-debugging#logging

Solution 4:[4]

If you're printing a lot of logs for debugging, it's better to use debugPrint(String) as mentioned in this answer. Printing numerous amount of lines could sometimes lead the Android kernel to drop some of the lines if you're hitting the limit.

If using print(String) is really needed for your use-case, you're right on adding the lint check for avoid_print since it seems to be set to true by default.

Solution 5:[5]

other answers helps to avoid the warning, but why this warning appear and how to fix it? using print method make your logs available to users when using flutter logs command, so if you log any sensitive data that would be dangerous, does debugPrint solve this issue and print only in debug mode? the answer is no, not by default, but the good point with debugPrint that you can override its behavior in a way that makes it not printing in release mode

void main() {
  if (kReleaseMode) {
    debugPrint = (String message, { int wrapWidth }) {} //
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 iDecode
Solution 2
Solution 3 Volodymyr Buberenko
Solution 4 Eslam Sameh Ahmed
Solution 5 Hazem Ashraf