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
recommended

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

example.dartdart
// 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);
}
Use a function declaration to bind a function to a name.
4void f() {
5 final greet = () { print('hi'); };
Use a function declaration to bind a function to a name.
5 final greet = () { print('hi'); };
6 final add = (int a, int b) => a + b;
Use a function declaration to bind a function to a name.
6 final add = (int a, int b) => a + b;
7 final square = (int x) => x * x;
Use a function declaration to bind a function to a name.
7 final square = (int x) => x * x;
8 final log = (String m) { print(m); };
Use a function declaration to bind a function to a name.
8 final log = (String m) { print(m); };
9 final wrap = () async { await g(); };
Use a function declaration to bind a function to a name.
9 final wrap = () async { await g(); };
10 final noop = () {};

Valid

example.dartdart
// 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.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-function-declarations-over-variables": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/prefer-function-declarations-over-variables: <reason>
  • prefer-generic-function-type-aliasesprefer_generic_function_type_aliasesrecommended

    Flags 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-formalsprefer_initializing_formalsrecommended

    Prefer initializing formals when a parameter is copied directly into its field.

  • prefer-inlined-addsprefer_inlined_addsrecommended

    Flags an add cascade on a list literal whose element could be written inline.

  • prefer-int-literalsprefer_int_literals

    Flags a whole-number double literal, such as 1.0, where the context type is double so the int literal 1 means the same value.

  • prefer-interpolation-to-compose-stringsprefer_interpolation_to_compose_stringsrecommended

    Prefers string interpolation to composing strings with +.

  • prefer-is-emptyprefer_is_emptyrecommended

    Flags .length comparisons equivalent to a .isEmpty check.

  • prefer-is-not-emptyprefer_is_not_emptyrecommended

    Flags .length comparisons equivalent to a .isNotEmpty check.