Avoid `print` calls in production code while using flutter
Solution 1
Use debugPrint
instead. Just know that debugPrint
can only take a String?
so you might need to use toString()
or correctly format the data you are trying to display.
import 'package:flutter/foundation.dart';
debugPrint('This is a debugPrint');
Solution 2
Print()
is a function which helps you in debugging process, it's not a good idea to use it unconditionally, you may leak some sensitive information in Release version of your app
It's better to make sure that you're using this function in Debug mode
There are some options:
Using debugPrint()
instead of print()
debugPrint('some debug info ...')
Using assert()
function which will be called in Debug mode only
assert(() {
print('some debug info ...');
return true;
});
Checking Debug Mode before using print()
import 'package:flutter/foundation.dart';
...
if (kDebugMode) {
print('some debug info ...');
}
If you're sure that you have enough control on using print()
function, you can ignore that warning by using ignore
directive :
Use this on top of the file : // ignore_for_file: avoid_print
Or add this line before print()
function
// ignore: avoid_print
print('some debug info ...');
To ignore that warning for the whole project, you can add this lines to analysis_options.yaml
file of the project :
include: package:flutter_lints/flutter.yaml
linter:
rules:
avoid_print: false
Solution 3
You can use print statement in only debug mode when app run in release mode print statement avoid. Write print using below both method:-
import 'package:flutter/foundation.dart';
if (kDebugMode) {
print("Hello");
}
OR
debugPrint('This is a debugPrint');
dekerthesamurai
Updated on January 03, 2023Comments
-
dekerthesamurai over 1 year
My code keeps giving me a warning... Avoid
print
calls in production code.How do I resolve it?
-
nvoigt over 2 yearsDon't use print statements then? I'm really not sure how this is a real question.
-
lbarqueira over 2 yearsDoes this answer your question? Avoid `print` calls in production code. (Documentation)
-