max_parameters
Reports a function that has more parameters than the limit. Pass the values that belong together as one object, or split the function.
- Tier
- advisory
- Scope
- Dart
- Fix
- none
What it reports
Section titled “What it reports”A function with more parameters than the limit max. A function exactly at max is not reported. The violation is on the function’s name, and its message names the count and the limit:
This function has 8 parameters, which is over the limit of 6.
- Every parameter counts as one: positional, optional in
[ ], named in{ },this.widthandsuper.keytoo. A parameter that is a function counts as one. Type parameters do not count. See How the parameters are counted. - What is measured: every top-level function, method, setter and operator, also one with no body such as an abstract method; every constructor with a body; each local function and closure on its own. A closure’s violation is on its parameter list. A getter is never reported.
- A constructor with no body is not measured:
const Card({super.key, required this.title, …});with twelve named parameters is not reported. That is where a Flutter widget declares its properties.
Past a handful of parameters, calls stop being readable: resize(800, 600, true, false, 2) says nothing, and two arguments of the same type can swap places without an error. Values that travel together through three functions, a width with a height or a user with a locale, are one thing that has no name yet. The rule is advisory because the count alone proves nothing: named parameters with defaults read well at any number.
Example
Section titled “Example”max: 4Widget priceTag( int cents, String currency, String locale, bool showDecimals, Color color, double fontSize, FontWeight weight,) { final text = format(cents, currency, locale, showDecimals); final style = TextStyle(color: color, fontSize: fontSize, fontWeight: weight); return Text(text, style: style);}class Money { const Money(this.cents, this.currency);
final int cents; final String currency;}
Widget priceTag(Money price, MoneyFormat format, TextStyle style) { return Text(format.apply(price), style: style);}The amount and its currency were one value, the locale and the decimals one way of writing it, and the last three parameters a text style, which already had a type.
Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
max |
none in the rule; the presets give it | The largest number of parameters a function may have. A whole number, zero or more. |
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
The rule has no limit of its own. The presets give one:
| Preset | max |
|---|---|
core |
the rule is not in this preset |
recommended |
6 |
strict |
4 |
To change it:
assay: rules: max_parameters: max: 8Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression. It goes above the function’s name, where the violation is, or at the end of that line:
// The signature is the callback type of the charting package.// ignore: assay/max_parametersWidget buildTooltip(// ignore_for_file: assay/max_parameters silences it in a whole file, and // ignore: max_parameters without the prefix works too. See Suppressions.
Details
Section titled “Details”All cases
What is measured
Section titled “What is measured”Each of these is measured on its own:
- a top-level function, a method, a setter, an operator, also one with no body, such as an abstract method or an external function;
- a constructor with a body, braces after the parameter list, factory constructors included. One that only has initializers or only redirects has no body;
- a local function, declared inside another one;
- a closure. It has no name, so the violation is on its parameter list.
A getter has no parameters and is never reported. A function type is not a function: typedef Compare = int Function(int a, int b, int c, int d, int e); is not reported, nor is a parameter or field of such a type. Where no limit is in effect, the rule reports nothing.
An override is measured like any other method. Where the declaration it overrides is in the project, both are reported; where it is in a package, only the override is, and you cannot shorten it: suppress it there.
How the parameters are counted
Section titled “How the parameters are counted”The count is the number of parameters the function declares:
- Every kind counts as one: required positional, optional positional in
[ ], named in{ }, required or not. - A constructor parameter that initializes a field,
this.width, or is passed to the superclass,super.key, counts like any other. - A setter has one, and an operator has as many as it declares.
- A type parameter is not a parameter:
T pick<T, U>(T first, U second)has two. - A parameter that is a function counts as one, not with its own parameters:
void each(List<int> items, void Function(int item, int index) visit)has two. - The receiver of an extension method,
this, is not a parameter. - A record or an object that carries several values counts as one.
- The parameters of an extension type’s primary constructor and the fields of a record type are not parameters of a function.
So this function has six: two positional and four named.
Future<Page> load( Uri address, int number, { int size = 20, String? sortBy, bool descending = false, Duration timeout = const Duration(seconds: 10),}) async { … }A function declared inside another one has its own parameters, counted for it and not for the function around it.