use-function-type-syntax-for-parameters

Same as use_function_type_syntax_for_parameters in Dart lints

Group
Style
Path
lint/style/use-function-type-syntax-for-parameters
Default
On (recommended)
Origin
Dart lints
Rule
491 of 512
recommended

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. Diagnostics cover the complete parameter declaration.

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) {}
}

Configure use-function-type-syntax-for-parameters

Set its severity in falcon.json. See the configuration guide.

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

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

example.dartdart
// falcon-ignore lint/style/use-function-type-syntax-for-parameters: <reason>
  • use-if-null-to-convert-nulls-to-boolsuse_if_null_to_convert_nulls_to_boolsrecommended

    Prefers ?? to comparing a nullable bool against a boolean literal.

  • use-late-for-private-fields-and-variablesuse_late_for_private_fields_and_variables

    Flags a private nullable field or top-level variable that is only ever assigned non-null values and read through !, so it could be late with a non-nullable type.

  • use-named-constantsuse_named_constantsrecommended

    Prefers a class's predefined static constant to constructing an equal constant.

  • use-raw-stringsuse_raw_strings

    Flags string literals whose only escapes are \\ and \$, which a raw string would express without any backslash.

  • use-setters-to-change-propertiesuse_setters_to_change_properties

    Flags one-parameter void methods that only assign that parameter to a field.

  • use-spacer-as-expanded-childuse_spacer_as_expanded_childrecommendedflutter

    Flags an Expanded wrapping an empty SizedBox or Container that should be a Spacer.

  • use-string-in-part-of-directivesuse_string_in_part_of_directivesrecommended

    Flags a part of directive that names its library instead of using a string URI.

  • use-super-parametersuse_super_parametersrecommended

    Prefer super parameters when a constructor only forwards parameters to its superclass.