print and debugPrint in Flutter

 

test with print and debugPrint

Hello,

When I was looking at the do’s & dont’s posts about Flutter, there was a topic I came across: print and debugPrint. I use the console screen as much as putting breakpoints while writing a program to test with print, to see where I am in stages. However, if I proceed with print, I get the warning “Don’t invoke ‘print’ in production code”. If I write the same line with debugPrint, this warning disappears. So today I wanted to investigate the difference between these two, why one gives a warning and the other doesn’t. Let’s examine both in detail.

What is print?

print is the standard output function of the Dart language. It prints the text specified in the terminal or console.

void main() {
print("This is an example of the print function.");
}

What are the disadvantages?

  • The first disadvantage is that when we want to print long texts or too many lines of output, it can give an “output truncated” error in the console (this has happened to me before, I encountered it when I researched debugPrint). So, we can have problems when printing a lot of data at the same time.
  • The second issue is actually related to the first one. When we want to output a lot of data or a large amount of data, there can be a performance loss. So it is less efficient in terms of performance.

What is debugPrint?

debugPrint is a Flutter framework specific function optimized for Flutter applications. It prints text to the console in the same way, but prevents long text from being split and truncated.

void main() {
debugPrint("This is an example of the debugPrint function.");
}

What are the advantages?

  • The debugPrint function checks the line boundaries of text and splits the output into chunks if necessary. This way, long texts are printed without being properly truncated.
  • This makes debugPrint more performant when printing large data and does not negatively affect the performance of the application.
  • debugPrint limits the output to 800 characters by default. However, we can use debugPrint’s “wrapWidth” parameter for longer output.

wrapWidth Usage

The wrapWidth parameter of the debugPrint function allows us to control how long text is wrapped. Here wrap means to determine after how many characters each line is divided.

String longText = 'This is an example of a very long text.' * 20;  //This means printing the text 20 times in a row.

// debugPrint without wrapWidth (standard)
debugPrint("without wrapWidth:", wrapWidth: 800);
debugPrint(longText);

// usage of debugPrint with wrapWidth parameter (split after the 50th character)
debugPrint("\nwith wrapWidth (50 characters):", wrapWidth: 800);
debugPrint(longText, wrapWidth: 50);
  • Without wrapWidth: debugPrint is used with the default wrap width (800 characters).
  • With wrapWidth (50 Characters): the wrapWidth parameter is set to 50 characters, so that each line is split after 50 characters.

Below we will see the output of these codes: texts will move to a new line after the specified number of characters. This is particularly useful for making long log messages more readable.

without wrapWidth:
This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.This is an example of a very long text.

with wrapWidth:
This is an example of a very long text.This is an
example of a very long text.This is an example of
a very long text.This is an example of a very long
text.This is an example of a very long text.This
is an example of a very long text.This is an
example of a very long text.This is an example of
a very long text.This is an example of a very long
text.This is an example of a very long text.This
is an example of a very long text.This is an
example of a very long text.This is an example of
a very long text.This is an example of a very long
text.This is an example of a very long text.This
is an example of a very long text.This is an
example of a very long text.This is an example of
a very long text.This is an example of a very long
text.This is an example of a very long text.

Which one to use in which situation?

Short and Simple Printouts: For simple and short outputs we can still use print (if we don’t mind the yellow warning).
Long and Complex Output: When you need to print long text or a lot of data, it is wiser to use debugPrint.
General Use: It is still generally more convenient and reliable to use debugPrint when debugging Flutter projects.

As we can see, small changes in the application can make a big difference in performance and usability. After doing my research, I decided to use debugPrint instead of print in all my tests and got rid of the yellow warning. I hope this was useful.

Thank you in advance for your valuable feedback.

Selin.

Hiç yorum yok: