Skip to main content

avoid_unused_constructor_parameters

Details about the 'avoid_unused_constructor_parameters' diagnostic produced by the Dart analyzer.

The parameter '{0}' is not used in the constructor.

Description

#

The analyzer produces this diagnostic when a constructor parameter isn't used in the constructor body or initializer list, and isn't an initializing formal parameter (this.x), a super parameter (super.x), or a declaring parameter.

Parameters that are only declared but never referenced are likely unintentional and add unnecessary complexity to the constructor.

Examples

#

The following code produces this diagnostic because the parameter x isn't used in the constructor:

dart
class C {
  C(int x);
}

The following code produces this diagnostic because the parameter c isn't used in the constructor initializer list:

dart
class C {
  int x;

  C(int a, int b, int c): x = a + b {
    print(x);
  }
}

Common fixes

#

If the parameter is meant to initialize a field, then use an initializing formal parameter:

dart
class C {
  int x;

  C(this.x);
}

If the parameter isn't needed, then remove it:

dart
class C {
  int x;

  C(int a, int b): x = a + b {
    print(x);
  }
}