Rules / Style / use-function-type-syntax-for-parameters

use-function-type-syntax-for-parameters

lint/style/use-function-type-syntax-for-parameters
recommended
Same as use_function_type_syntax_for_parameters · Dart lints

Flags an old-style function-typed parameter written with inline parameter syntax.

Dart lets you declare a function-typed parameter two ways. The legacy form inlines the callee's own parameter list — void f(int g(int x)) — while the modern generic function type syntax writes the type up front: void f(int Function(int) g). The modern form is preferred because it reads as an ordinary typed parameter, composes with nullability (Function(int)?), and keeps the parameter name in its usual place. The rule detects the old form by the nested parameter list the parser attaches to it; the modern form parses as a plain parameter with a function type and is not flagged. Rewrite using Function type syntax.

Invalid

example.dartdart
// Old-style function-typed parameters.
void forEach(int f(int x)) {}
void run(void cb()) {}
int apply(int g(int a, int b)) => g(1, 2);
void sortBy(bool compare(int a, int b)) {}
void schedule(void task(), int delay) {}

class C {
  void method(String fn(int x)) {}
}
Use the generic function type syntax for parameters.
1// Old-style function-typed parameters.
2void forEach(int f(int x)) {}
Use the generic function type syntax for parameters.
2void forEach(int f(int x)) {}
3void run(void cb()) {}
Use the generic function type syntax for parameters.
3void run(void cb()) {}
4int apply(int g(int a, int b)) => g(1, 2);
Use the generic function type syntax for parameters.
4int apply(int g(int a, int b)) => g(1, 2);
5void sortBy(bool compare(int a, int b)) {}
Use the generic function type syntax for parameters.
5void sortBy(bool compare(int a, int b)) {}
6void schedule(void task(), int delay) {}
Use the generic function type syntax for parameters.
8class C {
9 void method(String fn(int x)) {}

Valid

example.dartdart
// Modern generic function type syntax for parameters, plus plain params.
void forEach(int Function(int x) f) {}
void run(void Function() cb) {}
int apply(int Function(int, int) g) => g(1, 2);
void sortBy(bool Function(int, int) compare) {}
void plain(int a, String b) {}

class C {
  void method(void Function() fn) {}
}

How to configure

Set the severity of use-function-type-syntax-for-parameters in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "use-function-type-syntax-for-parameters": "error"
      }
    }
  }
}