prefer-function-declarations-over-variables
Same as prefer_function_declarations_over_variables in Dart lints
- Group
- Style
- Path
lint/style/prefer-function-declarations-over-variables- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 363 of 512
Flags a local variable bound to a function literal that should be a local function declaration.
Binding a closure to a variable (final f = () {...};) hides that it is a
named function; a proper declaration (void f() {...}) reads more clearly,
supports recursion and generic type parameters, and gives a return type a
natural home. The rule only reports final or const local bindings, which
provably can never be reassigned. A plain var f = () {...} is left alone,
since proving it is never reassigned requires dataflow this syntax-only pass
does not perform.
Invalid
// Function literals bound to final/const locals should be declarations.
Future<void> g() async {}
void f() {
final greet = () { print('hi'); };
final add = (int a, int b) => a + b;
final square = (int x) => x * x;
final log = (String m) { print(m); };
final wrap = () async { await g(); };
final noop = () {};
print(add(1, 2) + square(3) + greet.hashCode + log.hashCode + wrap.hashCode + noop.hashCode);
}void f() { final greet = () { print('hi'); }; final greet = () { print('hi'); }; final add = (int a, int b) => a + b; final add = (int a, int b) => a + b; final square = (int x) => x * x; final square = (int x) => x * x; final log = (String m) { print(m); }; final log = (String m) { print(m); }; final wrap = () async { await g(); }; final wrap = () async { await g(); }; final noop = () {};Valid
// Proper local function declarations, and reassignable var bindings.
void f() {
void greet() { print('hi'); }
int add(int a, int b) => a + b;
var handler = () => 1;
handler = () => 2;
final count = 3;
final name = 'x';
final list = <int>[];
print(add(1, 2) + count + name.length + list.length + handler());
greet();
}Configure prefer-function-declarations-over-variables
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"prefer-function-declarations-over-variables": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/prefer-function-declarations-over-variables: <reason>More style rules
- prefer-generic-function-type-aliases
prefer_generic_function_type_aliasesrecommendedFlags old-style function typedefs written with inline parameter syntax.
- prefer-getter-over-method
Prefer getters for synchronous, zero-argument methods that only read a field.
- prefer-initializing-formals
prefer_initializing_formalsrecommendedPrefer initializing formals when a parameter is copied directly into its field.
- prefer-inlined-adds
prefer_inlined_addsrecommendedFlags an
addcascade on a list literal whose element could be written inline. - prefer-int-literals
prefer_int_literalsFlags a whole-number double literal, such as
1.0, where the context type isdoubleso the int literal1means the same value. - prefer-interpolation-to-compose-strings
prefer_interpolation_to_compose_stringsrecommendedPrefers string interpolation to composing strings with
+. - prefer-is-empty
prefer_is_emptyrecommendedFlags
.lengthcomparisons equivalent to a.isEmptycheck. - prefer-is-not-empty
prefer_is_not_emptyrecommendedFlags
.lengthcomparisons equivalent to a.isNotEmptycheck.