prefer-inlined-adds

Same as prefer_inlined_adds in Dart lints

Group
Style
Path
lint/style/prefer-inlined-adds
Default
On (recommended)
Origin
Dart lints
Rule
373 of 512
recommended

Flags an add cascade on a list literal whose element could be written inline.

Building a list by literal-then-cascade ([a]..add(b)) is more verbose than placing the element directly in the literal ([a, b]), and the inline form reads as a single collection rather than a construction followed by mutation. The rule matches each ..add(...) section cascaded onto a list literal that takes exactly one positional argument and no named arguments; multi-argument or named calls, and cascades on non-list receivers, are ignored.

Invalid

example.dartdart
void bad() {
  var a = [1]..add(2);
  var b = <int>[]..add(1);
  var c = []..add(1)..add(2);
  var d = ['a']..add('b');
  var e = <String>['x']..add('y');
}
Inline the added element into the collection literal instead of using `add`.
1void bad() {
2 var a = [1]..add(2);
Inline the added element into the collection literal instead of using `add`.
2 var a = [1]..add(2);
3 var b = <int>[]..add(1);
Inline the added element into the collection literal instead of using `add`.
3 var b = <int>[]..add(1);
4 var c = []..add(1)..add(2);
Inline the added element into the collection literal instead of using `add`.
3 var b = <int>[]..add(1);
4 var c = []..add(1)..add(2);
Inline the added element into the collection literal instead of using `add`.
4 var c = []..add(1)..add(2);
5 var d = ['a']..add('b');
Inline the added element into the collection literal instead of using `add`.
5 var d = ['a']..add('b');
6 var e = <String>['x']..add('y');

Valid

example.dartdart
void good() {
  final items = <int>[];
  var a = [1, 2, 3];
  items.add(4);
  var b = items..add(5);
  var c = [1]..addAll([2, 3]);
  var d = [1, 2]..sort();
}

Configure prefer-inlined-adds

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-inlined-adds": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/prefer-inlined-adds: <reason>
  • prefer-int-literalsprefer_int_literals

    Flags a whole-number double literal, such as 1.0, where the context type is double so the int literal 1 means the same value.

  • prefer-interpolation-to-compose-stringsprefer_interpolation_to_compose_stringsrecommended

    Prefers string interpolation to composing strings with +.

  • prefer-is-emptyprefer_is_emptyrecommended

    Flags .length comparisons equivalent to a .isEmpty check.

  • prefer-is-not-emptyprefer_is_not_emptyrecommended

    Flags .length comparisons equivalent to a .isNotEmpty check.

  • prefer-is-not-operatorprefer_is_not_operatorrecommended

    Flags a negated type test !(x is T) that should use the is! operator.

  • prefer-iterable-ofprefer-iterable-ofrecommended

    Flags .from collection constructors in favor of the .of constructor.

  • prefer-iterable-where-typeprefer_iterable_whereTyperecommended

    Flags .where((e) => e is T), which .whereType<T>() expresses directly.

  • prefer-lastprefer-last

    Flags manual last-element indexing in favor of the .last getter.