avoid-final-parameters

Same as avoid_final_parameters in Dart lints

Group
Style
Path
lint/style/avoid-final-parameters
Default
Off, opt in
Origin
Dart lints
Rule
65 of 512

Flags parameters declared final.

Parameters are rarely reassigned, and marking them final adds noise to every signature without telling readers much. The final keyword is reported on any parameter of a function, method, constructor, setter, function literal, or function-typed parameter:

void f(final int x) {}        // flagged
void g(int x) {}              // fine

Initializing formals (final this.x, final super.x) and function-typed parameters (final void cb()) are not reported, and Function(...) types cannot declare final parameters at all. From Dart 3.13, where final marks a declaring primary-constructor parameter, the rule is inactive.

Invalid

example.dartdart
// @dart = 3.12

void f(final int x) {}

void g(int a, [final int b = 0]) {}

void h({final String? name}) {}

void i({required final String id}) {}

class A {
  A(final int value);

  void m(final x) {}

  set value(final int v) {}

  int operator +(final int other) => other;
}

void j() {
  void local(final int x) {}
  final literal = (final int y) => y;
  local(1);
  literal(2);
}
Parameters should not be marked as `final`.
2
3void f(final int x) {}
Parameters should not be marked as `final`.
4
5void g(int a, [final int b = 0]) {}
Parameters should not be marked as `final`.
6
7void h({final String? name}) {}
Parameters should not be marked as `final`.
8
9void i({required final String id}) {}
Parameters should not be marked as `final`.
11class A {
12 A(final int value);
Parameters should not be marked as `final`.
13
14 void m(final x) {}
Parameters should not be marked as `final`.
15
16 set value(final int v) {}
Parameters should not be marked as `final`.
17
18 int operator +(final int other) => other;
Parameters should not be marked as `final`.
21void j() {
22 void local(final int x) {}
Parameters should not be marked as `final`.
22 void local(final int x) {}
23 final literal = (final int y) => y;

Valid

example.dartdart
void f(int x) {}

void g(int a, [int b = 0]) {}

void h({String? name}) {}

class A {
  final int value;
  final int other;

  // Initializing formals may be final.
  A(final this.value, this.other);

  void m(int x) {
    final y = x;
    print(y);
  }

  set setter(int v) {}
}

class B extends A {
  // Super parameters may be final.
  B(final super.value, super.other);
}

// Function-typed parameters are not reported.
void callback(final void cb()) {}

void j() {
  final local = 1;
  print(local);
}

Configure avoid-final-parameters

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-final-parameters": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/avoid-final-parameters: <reason>