use-named-constants

Same as use_named_constants in Dart lints

Group
Style
Path
lint/style/use-named-constants
Default
On (recommended)
Origin
Dart lints
Rule
496 of 512
recommended

Prefers a class's predefined static constant to constructing an equal constant.

When a class declares static const zero = A(0);, writing const A(0) elsewhere creates the very same canonicalized object; referring to A.zero names the intent. The rule fires on a constant constructor invocation (an explicit const creation or a constructor call in a constant context) whose value equals an accessible static const field of the constructed class. An invocation inside a field initializer of that same class is exempt, so static const a = A(); static const b = A(); is fine.

falcon compares canonical constant values with its conservative evaluator. Named and redirecting constructors are compared by their resulting instance, not their constructor spelling. Unavailable declarations and values the evaluator cannot fold are not checked.

Invalid

example.dartdart
class Point {
  const Point(this.x, this.y);
  final int x;
  final int y;

  static const origin = Point(0, 0);
  static const unitX = Point(1, 0);
}

class Flag {
  const Flag();
  const Flag.named();

  static const instance = Flag();
}

const a = Point(0, 0);

void main() {
  const b = Point(1, 0);
  final c = const Point(0, 0);
  print(const Flag());
  const list = [Point(0, 0)];
  print([a, b, c, list, const Flag.named()]);
}

class Holder {
  const Holder([this.p = const Point(1, 0)]);
  final Point p;
}

class Initialized {
  const Initialized(this.x);
  const Initialized.named() : x = 0;
  final int x;
  static const zero = Initialized(0);
}

const sameFields = Initialized.named();

abstract class NamedSequence<T> {
  const factory NamedSequence.single(T value) = SingleSequence<T>;
  static const trueSequence = SingleSequence<bool>(true);
  static const emptyString = SingleSequence<String>('');
}

class SingleSequence<T> implements NamedSequence<T> {
  const SingleSequence(this.value);
  final T value;
}

const redirectedBool = NamedSequence.single(true);
const redirectedString = NamedSequence.single('');

const Initialized contextualShorthand = .named();
final Initialized explicitShorthand = const .named();
Use the constant `Point.origin` rather than a constructor returning the same object.
16
17const a = Point(0, 0);
Use the constant `Point.unitX` rather than a constructor returning the same object.
19void main() {
20 const b = Point(1, 0);
Use the constant `Point.origin` rather than a constructor returning the same object.
20 const b = Point(1, 0);
21 final c = const Point(0, 0);
Use the constant `Flag.instance` rather than a constructor returning the same object.
21 final c = const Point(0, 0);
22 print(const Flag());
Use the constant `Point.origin` rather than a constructor returning the same object.
22 print(const Flag());
23 const list = [Point(0, 0)];
Use the constant `Flag.instance` rather than a constructor returning the same object.
23 const list = [Point(0, 0)];
24 print([a, b, c, list, const Flag.named()]);
Use the constant `Point.unitX` rather than a constructor returning the same object.
27class Holder {
28 const Holder([this.p = const Point(1, 0)]);
Use the constant `Initialized.zero` rather than a constructor returning the same object.
38
39const sameFields = Initialized.named();
Use the constant `NamedSequence.trueSequence` rather than a constructor returning the same object.
51
52const redirectedBool = NamedSequence.single(true);
Use the constant `NamedSequence.emptyString` rather than a constructor returning the same object.
52const redirectedBool = NamedSequence.single(true);
53const redirectedString = NamedSequence.single('');
Use the constant `Initialized.zero` rather than a constructor returning the same object.
54
55const Initialized contextualShorthand = .named();
Use the constant `Initialized.zero` rather than a constructor returning the same object.
55const Initialized contextualShorthand = .named();
56final Initialized explicitShorthand = const .named();

Valid

example.dartdart
class Point {
  const Point(this.x, this.y);
  final int x;
  final int y;

  static const origin = Point(0, 0);
  static const alsoOrigin = Point(0, 0);
  static const _secret = Point(9, 9);
}

class Flag {
  const Flag(this.on);
  final bool on;

  static const yes = Flag(true);
}

void main() {
  const notNamed = Point(2, 3);
  final notConst = Point(0, 0);
  const otherFlag = Flag(false);
  final runtimeFlag = Flag(true);
  const viaName = Point.origin;
  print([notNamed, notConst, otherFlag, runtimeFlag, viaName, Point._secret]);
}

class Different {
  const Different(this.value);
  const Different.one() : value = 1;
  final int value;
  static const zero = Different(0);
}

const unequalFields = Different.one();

Configure use-named-constants

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "use-named-constants": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/use-named-constants: <reason>
  • use-raw-stringsuse_raw_strings

    Flags string literals whose only escapes are \\ and \$, which a raw string would express without any backslash.

  • use-setters-to-change-propertiesuse_setters_to_change_properties

    Flags one-parameter void methods that only assign that parameter to a field.

  • use-spacer-as-expanded-childuse_spacer_as_expanded_childrecommendedflutter

    Flags an Expanded wrapping an empty SizedBox or Container that should be a Spacer.

  • use-string-in-part-of-directivesuse_string_in_part_of_directivesrecommended

    Flags a part of directive that names its library instead of using a string URI.

  • use-super-parametersuse_super_parametersrecommended

    Prefer super parameters when a constructor only forwards parameters to its superclass.

  • use-test-throws-matchersuse_test_throws_matchersrecommended

    Prefers throwsA matchers over try { ...; fail(...); } catch (...) {}.

  • use-to-and-as-if-applicableuse_to_and_as_if_applicable

    Flags conversion methods not named to… or as….

  • always-declare-return-typesalways_declare_return_typesrecommended

    Flags functions and methods declared without a return type.