avoid_print
避免在生产代码中调用 print
。
详情
#应该 避免在生产代码中调用 print
。
对于生产代码,请考虑使用日志框架。如果您正在使用 Flutter,则可以使用 debugPrint
或将 print
调用置于 kDebugMode
的检查中
错误示例
dart
void f(int x) {
print('debug: $x');
...
}
正确示例
dart
void f(int x) {
debugPrint('debug: $x');
...
}
正确示例
dart
void f(int x) {
log('log: $x');
...
}
正确示例
dart
void f(int x) {
if (kDebugMode) {
print('debug: $x');
}
...
}
启用
#要启用 avoid_print
规则,请在您的 analysis_options.yaml
文件中的 linter > rules 下添加 avoid_print
analysis_options.yaml
yaml
linter:
rules:
- avoid_print
如果您改为使用 YAML 映射语法来配置 linter 规则,请在 linter > rules 下添加 avoid_print: true
analysis_options.yaml
yaml
linter:
rules:
avoid_print: true
除非另有说明,否则本网站上的文档反映了 Dart 3.7.1 版本。页面上次更新于 2025-03-07。 查看源代码 或 报告问题。