static_field_without_shorthand
Reports a class name written before its own static field where the place already expects that class, as in fontWeight: FontWeight.w600. Use the dot shorthand: fontWeight: .w600.
- Tier
- advisory
- Scope
- Dart
- Lowest preset
- strict
- Fix
- deletes the class name and keeps the dot
What it reports
Section titled “What it reports”The name of a class written before one of its own static fields, or a static getter, which reads as one, where the place already expects that class: job(size: Size.small) for a parameter of type Size, width: double.infinity, fontWeight: FontWeight.w600. The violation covers the class name, with any import prefix.
It reports only where the library’s language version has dot shorthands, and only where the expected type is written, possibly nullable:
- an argument for a parameter declared with the class as its type (not a type parameter);
- the initializer of a variable or field declared with it, or the right side of an assignment to one;
- a parameter’s default value;
- what a function or method declared to return it gives with
returnor=>, unless it isasyncor a generator.
It does not report:
- the values of an enum: whether to write the enum’s name is left to you;
- a field of another class:
Colors.redfor aColor; - an untyped variable or a generic parameter.
The fix deletes the class name and keeps the dot. It is not offered where a comment stands before the dot.
A dot shorthand looks the name up in the type the place expects. There, the class name says nothing the place does not.
Example
Section titled “Example”Text(name, style: TextStyle(fontWeight: FontWeight.w600));Text(name, style: TextStyle(fontWeight: .w600));Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression:
// Why it stays as written here.// ignore: assay/static_field_without_shorthand// ignore_for_file: assay/static_field_without_shorthand silences it in a whole file, and // ignore: static_field_without_shorthand without the prefix works too. See Suppressions.