prefer-constructors-over-static-methods
Same as prefer_constructors_over_static_methods in Dart lints
- Group
- Style
- Path
lint/style/prefer-constructors-over-static-methods- Default
- Off, opt in
- Origin
- Dart lints
- Rule
- 332 of 512
Flags static methods that create and return an instance of their own class.
A static method whose job is to build an instance of the enclosing type is a named constructor in disguise. Named (and factory) constructors make the intent explicit, show up as constructors in tooling, and work with constructor tear-offs.
class Point {
final num x, y;
Point(this.x, this.y);
static Point polar(num theta, num radius) => // flagged
Point(radius * cos(theta), radius * sin(theta));
Point.fromPolar(num theta, num radius) // preferred
: x = radius * cos(theta), y = radius * sin(theta);
}
Only non-generic static methods and getters of non-generic classes and extension types are checked. As in the analyzer, the last visited constructor call determines whether the body creates the declared enclosing return type.
Invalid
class Point {
final num x;
final num y;
Point(this.x, this.y);
static Point polar(num theta, num radius) =>
Point(radius * theta, radius * theta);
static Point origin() {
return Point(0, 0);
}
static Point fromList(List<num> xs) {
final p = Point(xs[0], xs[1]);
return p;
}
}
class Named {
final String name;
Named._(this.name);
static Named create(String name) => Named._(name.trim());
}
class WithNew {
WithNew();
static WithNew make() => new WithNew();
}
class Implicit {
static Implicit build() => Implicit();
}
class Singleton {
static Singleton get instance => Singleton();
}
class NamedCreation {
const NamedCreation.fallback();
static NamedCreation of(Object? value) => value as NamedCreation? ?? const NamedCreation.fallback();
}
class PrunedArguments {
PrunedArguments(Object value);
static PrunedArguments create() => PrunedArguments(Object());
}
class ClosureCreation {
static ClosureCreation create() {
final make = () => ClosureCreation();
return make();
}
} static Point polar(num theta, num radius) => static Point origin() { static Point fromList(List<num> xs) { static Named create(String name) => Named._(name.trim()); static WithNew make() => new WithNew();class Implicit { static Implicit build() => Implicit();class Singleton { static Singleton get instance => Singleton(); const NamedCreation.fallback(); static NamedCreation of(Object? value) => value as NamedCreation? ?? const NamedCreation.fallback(); PrunedArguments(Object value); static PrunedArguments create() => PrunedArguments(Object());class ClosureCreation { static ClosureCreation create() {Valid
class Point {
final num x;
final num y;
Point(this.x, this.y);
Point.polar(num theta, num radius)
: x = radius * theta,
y = radius * theta;
// Nullable return type: a constructor cannot return null.
static Point? tryParse(String text) => text.isEmpty ? null : Point(0, 0);
// Generic static method.
static Point first<T>(List<T> xs) => Point(xs.length, 0);
// Returns a cached instance without constructing one.
static final Point _zero = Point(0, 0);
static Point zero() => _zero;
// Instance method creating a new instance.
Point translate(num dx) => Point(x + dx, y);
// Returns another type.
static String describe() => Point(0, 0).toString();
}
// Generic classes are skipped.
class Box<T> {
final T value;
Box(this.value);
static Box<int> ofInt() => Box<int>(0);
}
// Factory constructor already.
class Logger {
Logger._();
factory Logger.create() => Logger._();
}
// Returning a subtype instance.
class Shape {
static Shape square() => Square();
}
class Square extends Shape {}
// The analyzer's visitor overwrites earlier matching constructor calls.
class LastCreation {
static LastCreation create() {
final value = LastCreation();
Object();
return value;
}
}
class DefaultParameterOnly {
const DefaultParameterOnly();
static DefaultParameterOnly create([
DefaultParameterOnly value = const DefaultParameterOnly(),
]) => value;
}
class ClosureOverwrite {
static ClosureOverwrite create() {
final value = ClosureOverwrite();
final other = () => Object();
return value;
}
}Configure prefer-constructors-over-static-methods
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"prefer-constructors-over-static-methods": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/prefer-constructors-over-static-methods: <reason>More style rules
- prefer-containerflutter
Suggests collapsing compatible nested layout widgets into one
Container. - prefer-correct-callback-field-name
Match callback field names to a configured regular expression.
- prefer-correct-error-name
Require configured error parameter names in catches and SDK error callbacks.
- prefer-correct-handler-name
Match callback handler methods to a configured regular expression.
- prefer-correct-identifier-length
prefer-correct-identifier-lengthFlags declared identifiers that are shorter or longer than the allowed range.
- prefer-correct-notifier-file-nameriverpod
Requires
snake_casefile names ending in _notifier.dart for resolved Riverpod notifier classes. - prefer-correct-provider-file-nameriverpod
Requires
snake_casefile names ending in _provider.dart for resolved Riverpod provider declarations, including annotated generator functions. - prefer-correct-test-file-nametest
Requires entry-point files under package-root test/ or
integration_test/ to use the _test.dart suffix.