avoid-print

Same as avoid_print in Dart lints

Path
lint/suspicious/avoid-print
Default
On (recommended)
Origin
Dart lints
Domain
flutter
Rule
114 of 512
recommendedflutter

Flags calls and callback arguments resolving to dart:core's print. Lexical bindings and members declared in the enclosing body shadow it. Calls guarded by Flutter's kDebugMode are exempt within the same function body. Diagnostics underline the print identifier, not the invocation or its arguments.

Invalid

example.dartdart
// Bad: calls to the top-level print function.
void main() {
  print('hello');
  print(42);
  const value = 7;
  print(value);
  print('sum: ${value + 1}');
  final list = [1, 2, 3];
  list.forEach((e) => print(e));
  print('done');
}

// A local shadow declared in a try, catch or finally body is scoped to that
// body and must not suppress the call after the statement.
void scopedShadow() {
  try {
    void print(Object? value) {}
    print('shadowed');
  } catch (_) {
    void print(Object? value) {}
    print('shadowed');
  } finally {
    void print(Object? value) {}
    print('shadowed');
  }
  print('real');
}
Avoid using `print` in production code; use a logging framework instead.
2void main() {
3 print('hello');
Avoid using `print` in production code; use a logging framework instead.
3 print('hello');
4 print(42);
Avoid using `print` in production code; use a logging framework instead.
5 const value = 7;
6 print(value);
Avoid using `print` in production code; use a logging framework instead.
6 print(value);
7 print('sum: ${value + 1}');
Avoid using `print` in production code; use a logging framework instead.
8 final list = [1, 2, 3];
9 list.forEach((e) => print(e));
Avoid using `print` in production code; use a logging framework instead.
9 list.forEach((e) => print(e));
10 print('done');
Avoid using `print` in production code; use a logging framework instead.
25 }
26 print('real');

Valid

example.dartdart
// Good: no calls to the top-level print function.
void main() {
  debugPrint('hello');
  logger.info('message');
  final printer = Printer();
  printer.print('doc');
  stdout.write('line');
  final summary = describe();
  logger.info(summary);
}

void debugPrint(String s) {}

class Printer {
  void print(String s) {}
}

class Logger {
  void info(String s) {}
}

final logger = Logger();
final stdout = StringSink();

class StringSink {
  void write(String s) {}
}

String describe() => 'ok';

void shadowedByParameter(void Function(Object?) print) {
  print('user function');
}

class HasPrintMember {
  void print(Object? value) {}

  void run() {
    print('member');
  }
}

Configure avoid-print

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "avoid-print": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/suspicious/avoid-print: <reason>