48

Is there a way to disable a linting rule for a line in flutter?

I have a specific use case where I want to disable linting for two lines. I have a lot of business logic already written so I cannot change the code.

abstract class ReviewName {
  static final NEW = 'NEW';
  static final OLD = 'OLD';
}

The above code will have linting errors: Name non-constant identifiers using lowerCamelCase.dart(non_constant_identifier_names)

Is there any way I avoid the lint error for only the two lines?

SRAVAN
  • 769
  • 1
  • 7
  • 11

2 Answers2

87

General answer

To ignore a single line, you can add a comment above the line:

// ignore: non_constant_identifier_names
final NEW = 'NEW';

To ignore for the whole file, you can add a comment at the top of the file:

// ignore_for_file: non_constant_identifier_names

To ignore for the whole project, you can set the rule to false in your analysis_options.yaml file:

include: package:lints/recommended.yaml

linter:
  rules:
    non_constant_identifier_names: false

See also

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
26

Use the // ignore: syntax, for example:

abstract class ReviewName {
  // ignore: non_constant_identifier_names
  static final NEW = 'NEW';

  // ignore: non_constant_identifier_names
  static final OLD = 'OLD';
}

The list of rule names is here.

Richard Heap
  • 48,344
  • 9
  • 130
  • 112
  • 1
    I found the documentation here: https://dart.dev/guides/language/analysis-options#excluding-code-from-analysis – SRAVAN May 31 '19 at 14:21
  • I would wish for something more global. I love snake_case, but dart community disagree. – Mathieu J. Apr 26 '20 at 03:37
  • The OP wanted just two lines. The global way is to change `analysis.yaml`. Where possible try to respect the community conventions for code readability. – Richard Heap Apr 26 '20 at 10:24
  • 1
    Any way just add one //ignore for the whole class? If I have 50 static strings I don't wanna add extra 50 comments. – Chandler Jun 02 '21 at 10:45