Reputation: 29016
I started seeing this warning in all my print
statements.
print('Foo'); // Warning: Avoid `print` calls in production code.
Upvotes: 40
Views: 42629
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
Upvotes: 1
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
Reputation: 683
Simply do try as below
import 'dart:developer' as developer;
...
developer.log("log instead of print");
Upvotes: 1
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.
To remove the warning in that single line:
// ignore: avoid_print
print('Hello World');
To remove the warning in that file
// ignore_for_file: avoid_print
print('Hello World');
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
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.
String
as an argument, unlike print
which accepts an Object?
(everything)Upvotes: 50
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
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
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
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
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