Skip to content

prefer_returning_shorthands

v0.3.0 Warning Fix Shorthand Patterns

Flags expression function bodies that return an instance whose type matches the declared return type. Since the return type is already explicit, the class name in the constructor call is redundant and can be replaced with a dot shorthand (e.g., .new() or .named()).

When a function already declares its return type, repeating the class name in the returned constructor call adds visual noise without extra information. Dot shorthands reduce this redundancy, making arrow functions more concise. This also applies to both branches of conditional expressions.

See also: Dart language - Arrow syntax

class SomeClass {
final String value;
const SomeClass(this.value);
const SomeClass.named(this.value);
}
SomeClass getInstance() => SomeClass('val');
SomeClass getNamedInstance() => SomeClass.named('val');
SomeClass getConditional(bool flag) =>
flag ? SomeClass('value') : SomeClass.named('val');
SomeClass? getNullable() => SomeClass('val');
SomeClass getInstance() => .new('val');
SomeClass getNamedInstance() => .named('val');
SomeClass getConditional(bool flag) =>
flag ? .new('value') : .named('val');
// Block function bodies are not flagged:
SomeClass getWithBlock() {
return SomeClass('val');
}
// No explicit return type — not flagged:
getInstance() => SomeClass('val');
// Dynamic return type — not flagged:
dynamic getDynamic() => SomeClass('val');

To disable this rule:

plugins:
many_lints:
diagnostics:
prefer_returning_shorthands: false