Skip to main content

unnecessary-trailing-comma

effort: 2m
configurable
has IDE fix
has auto-fix
pro+

Checks for unnecessary trailing commas for arguments, parameters, enum values and collections.

Removing those commas results in dart format producing code that fits on one line.

note

Use this rule only with the trailing_commas: preserve mode enabled. Otherwise the formatter will change trailing commas for you.

⚙️ Config

Set max-width (default is 80) to configure a custom max line width.

Set multiline (default is false) to highlight an unnecessary comma for arguments that take more than one line and are last (example).

Set trailing-lists (default is false) to highlight an unnecessary comma for list, set or map literals that are the last argument of an invocation (example).

analysis_options.yaml
dcm:
rules:
- unnecessary-trailing-comma:
max-width: 80
multiline: false
trailing-lists: false

Example

❌ Bad:

void function(
String first,
String second, // LINT: Avoid unnecessary trailing comma. Try removing it.
) {
return;
}

✅ Good:

void function(String first, String second) {
return;
}

Example with "multiline"

Config
analysis_options.yaml
dcm:
rules:
- unnecessary-trailing-comma:
multiline: true

❌ Bad:

Invocation().thisOrAncestorMatching(
(someObject) => someObject is List<int>
? someObject.length
: int.tryParse(someObject.toString()), // LINT: Avoid unnecessary trailing comma. Try removing it.
);

✅ Good:

Invocation().thisOrAncestorMatching((someObject) => someObject is List<int>
? someObject.length
: int.tryParse(someObject.toString()));

Example with "trailing-lists"

Config
analysis_options.yaml
dcm:
rules:
- unnecessary-trailing-comma:
trailing-lists: true

❌ Bad:

withNamedList(
name: '',
values: [
true,
false,
], // LINT: Avoid unnecessary trailing comma. Try removing it.
);

✅ Good:

withNamedList( name: '', values: [
true,
false,
]);