iDecode
iDecode

Reputation: 29016

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. 

Upvotes: 40

Views: 42629

Answers (9)

Heshan Sandeepa
Heshan Sandeepa

Reputation: 3687

flutter enforces avoiding print statements after Dart version 2.5.0 read here

you can fix that by either checking the 'kDebugMode' or using debugPrint statements.

void f(int x) {
   debugPrint('debug: $x');
}

or

void f(int x) {
 if (kDebugMode) {
     print('debug: $x');
 }
}

A few things to consider

  1. both print & debug print work in all modes ( debug, profile or release ).
  2. avoid any sort of logging in production code as that would be a waste of resources.
  3. if the content is too long, print statements will stop logging after a certain length. The same goes with debug print , but can be avoided by using 'wrapWidth' property

Upvotes: 1

Akshit Tyagi
Akshit Tyagi

Reputation: 220

I use to create a common function

void log(message) => print(message);

and whenever need to print something I just call log("Hello Word") and when I deploy, I make this function null. As print statement also reduces the performance of the app.

Upvotes: 4

Zia
Zia

Reputation: 683

Simply do try as below

import 'dart:developer' as developer;

...

developer.log("log instead of print");

Upvotes: 1

iDecode
iDecode

Reputation: 29016

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
    

Why not debugPrint or log?

Although you can also use debugPrint, or log (from dart:developer), but there are a few reasons why I don't like them.

  1. They both work only in Flutter apps (not Dart apps)
  2. You'll have to manually import a library for them to work (importing is such a pain unless imports on fly is implemented in IDE)
  3. They accept only a String as an argument, unlike print which accepts an Object? (everything)

Upvotes: 50

Elmar
Elmar

Reputation: 4455

Null safe working version in 2022:

import 'package:flutter/foundation.dart';


void main() async {
   if (kReleaseMode) {
    debugPrint = (String? message, {int? wrapWidth}) => '';
  }

Upvotes: 0

Volodymyr Buberenko
Volodymyr Buberenko

Reputation: 668

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 the logged event 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

Upvotes: 25

Kobi
Kobi

Reputation: 455

Replace all print( to logPrint(

Create a new Function

logPrint(String s){print(s);}

And you can remark all the print commands in one line

Upvotes: -2

Omatt
Omatt

Reputation: 10539

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.

Upvotes: 4

Hazem Ashraf
Hazem Ashraf

Reputation: 340

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 }) {} //
  }
}

Upvotes: 0

Related Questions