Skip to main content

parameter_assignments

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

Invalid assignment to the parameter '{0}'.

Description

#

The analyzer produces this diagnostic when a parameter is reassigned within a function body. Reassigning parameters can make it harder to understand what value a parameter holds at any given point in a function.

All types of assignments are considered, including:

  • Direct variable assignments (with =).
  • Null-aware assignments (with ??=).
  • Compound assignments (such as with +=).
  • Increment and decrement operators (such as with ++).
  • Variable pattern assignments (such as in var (a, b) = (1, 2);).

There's an exception for a single null-aware assignment (??=) at the beginning of a function for nullable parameters that don't have a default value, as this is a common pattern for providing defaults.

Examples

#

The following code produces this diagnostic because the parameter x is reassigned a new value:

dart
void f(int x) {
  x = 4;
  print(x);
}

The following code produces this diagnostic because the parameter x is modified with a postfix increment:

dart
void f(int x) {
  x++;
}

Common fixes

#

Introduce a local variable to hold the modified value instead of reassigning the parameter:

dart
void f(int x) {
  var y = x + 4;
  print(y);
}