prefer-correct-edge-insets-constructor

Same as prefer-correct-edge-insets-constructor in Dart Code Metrics

Path
lint/performance/prefer-correct-edge-insets-constructor
Default
On (recommended)
Origin
Dart Code Metrics
Domain
flutter
Rule
336 of 512
recommendedflutter

Flags an EdgeInsets.only(...) that a more specific constructor expresses better.

EdgeInsets.only with symmetric values is clearer and cheaper as EdgeInsets.all(n) (all four sides equal), EdgeInsets.symmetric(vertical: n) (only top and bottom, equal), or EdgeInsets.symmetric(horizontal: n) (only left and right, equal). The rule detects these three shapes on EdgeInsets.only(...) (and a direct EdgeInsets(...) carrying the same named arguments) and reports the equivalent to prefer.

Invalid

example.dartdart
// Test cases for prefer-correct-edge-insets-constructor rule
// Flags EdgeInsets.only() where a simpler constructor should be used

void testSymmetricVertical() {
  final padding = EdgeInsets.only(top: 8, bottom: 8);
}

void testSymmetricHorizontal() {
  final padding = EdgeInsets.only(left: 4, right: 4);
}

void testAllEqual() {
  final padding = EdgeInsets.only(left: 4, top: 4, right: 4, bottom: 4);

  final padding2 = EdgeInsets.only(
    left: 16,
    top: 16,
    right: 16,
    bottom: 16,
  );
}

void testMultipleViolations() {
  final pad1 = EdgeInsets.only(top: 10, bottom: 10);
  final pad2 = EdgeInsets.only(left: 8, right: 8);
  final pad3 = EdgeInsets.only(left: 12, top: 12, right: 12, bottom: 12);
}

class MyWidget {
  final padding = EdgeInsets.only(top: 20, bottom: 20);
}

// Regression: the violation must still be found inside Dart 3 containers
// (pattern declaration, labeled statement, switch expression and subject,
// collection if/spread, record field).
void containersRegression(int rcount) {
  final (ra, _) = (EdgeInsets.only(top: 8, bottom: 8), 0);
  lbl: {
    final rb = EdgeInsets.only(top: 8, bottom: 8);
    print(rb);
  }
  final rc = switch (rcount) {
    0 => EdgeInsets.only(top: 8, bottom: 8),
    _ => null,
  };
  final rd = switch (EdgeInsets.only(top: 8, bottom: 8)) {
    _ => 0,
  };
  final re = [if (rcount > 0) EdgeInsets.only(top: 8, bottom: 8)];
  final rf = [...[EdgeInsets.only(top: 8, bottom: 8)]];
  final rg = (p: EdgeInsets.only(top: 8, bottom: 8), q: 0);
  print([ra, rc, rd, re, rf, rg]);
}
`EdgeInsets.only()` should use `EdgeInsets.symmetric()` or `EdgeInsets.all()`.
4void testSymmetricVertical() {
5 final padding = EdgeInsets.only(top: 8, bottom: 8);
`EdgeInsets.only()` should use `EdgeInsets.symmetric()` or `EdgeInsets.all()`.
8void testSymmetricHorizontal() {
9 final padding = EdgeInsets.only(left: 4, right: 4);
`EdgeInsets.only()` should use `EdgeInsets.symmetric()` or `EdgeInsets.all()`.
12void testAllEqual() {
13 final padding = EdgeInsets.only(left: 4, top: 4, right: 4, bottom: 4);
`EdgeInsets.only()` should use `EdgeInsets.symmetric()` or `EdgeInsets.all()`.
14
15 final padding2 = EdgeInsets.only(
`EdgeInsets.only()` should use `EdgeInsets.symmetric()` or `EdgeInsets.all()`.
23void testMultipleViolations() {
24 final pad1 = EdgeInsets.only(top: 10, bottom: 10);
`EdgeInsets.only()` should use `EdgeInsets.symmetric()` or `EdgeInsets.all()`.
24 final pad1 = EdgeInsets.only(top: 10, bottom: 10);
25 final pad2 = EdgeInsets.only(left: 8, right: 8);
`EdgeInsets.only()` should use `EdgeInsets.symmetric()` or `EdgeInsets.all()`.
25 final pad2 = EdgeInsets.only(left: 8, right: 8);
26 final pad3 = EdgeInsets.only(left: 12, top: 12, right: 12, bottom: 12);
`EdgeInsets.only()` should use `EdgeInsets.symmetric()` or `EdgeInsets.all()`.
29class MyWidget {
30 final padding = EdgeInsets.only(top: 20, bottom: 20);
`EdgeInsets.only()` should use `EdgeInsets.symmetric()` or `EdgeInsets.all()`.
36void containersRegression(int rcount) {
37 final (ra, _) = (EdgeInsets.only(top: 8, bottom: 8), 0);
`EdgeInsets.only()` should use `EdgeInsets.symmetric()` or `EdgeInsets.all()`.
38 lbl: {
39 final rb = EdgeInsets.only(top: 8, bottom: 8);
`EdgeInsets.only()` should use `EdgeInsets.symmetric()` or `EdgeInsets.all()`.
42 final rc = switch (rcount) {
43 0 => EdgeInsets.only(top: 8, bottom: 8),
`EdgeInsets.only()` should use `EdgeInsets.symmetric()` or `EdgeInsets.all()`.
45 };
46 final rd = switch (EdgeInsets.only(top: 8, bottom: 8)) {
`EdgeInsets.only()` should use `EdgeInsets.symmetric()` or `EdgeInsets.all()`.
48 };
49 final re = [if (rcount > 0) EdgeInsets.only(top: 8, bottom: 8)];
`EdgeInsets.only()` should use `EdgeInsets.symmetric()` or `EdgeInsets.all()`.
49 final re = [if (rcount > 0) EdgeInsets.only(top: 8, bottom: 8)];
50 final rf = [...[EdgeInsets.only(top: 8, bottom: 8)]];
`EdgeInsets.only()` should use `EdgeInsets.symmetric()` or `EdgeInsets.all()`.
50 final rf = [...[EdgeInsets.only(top: 8, bottom: 8)]];
51 final rg = (p: EdgeInsets.only(top: 8, bottom: 8), q: 0);

Valid

example.dartdart
// Good examples for prefer-correct-edge-insets-constructor rule
// Using simpler constructors when appropriate

void testSymmetricConstructor() {
  final padding1 = EdgeInsets.symmetric(vertical: 8);
  final padding2 = EdgeInsets.symmetric(horizontal: 4);
  final padding3 = EdgeInsets.symmetric(vertical: 10, horizontal: 16);
}

void testAllConstructor() {
  final padding = EdgeInsets.all(4);
  final padding2 = EdgeInsets.all(16);
}

void testOnlyWithAsymmetricValues() {
  final padding = EdgeInsets.only(
    top: 8,
    bottom: 12,
    left: 4,
    right: 6,
  );

  final padding2 = EdgeInsets.only(top: 10, left: 5);
}

void testZeroConstructors() {
  final padding1 = EdgeInsets.zero;
  final padding2 = EdgeInsets.only(left: 8, right: 16);
}

Configure prefer-correct-edge-insets-constructor

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "performance": {
        "prefer-correct-edge-insets-constructor": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/performance/prefer-correct-edge-insets-constructor: <reason>
  • prefer-declaring-const-constructorprefer_declaring_const_constructorflutter

    Flags a generative constructor that is legally eligible to be const.

  • prefer-foreachprefer_foreach

    Prefers forEach to a for-in loop that only passes each element to a function.

  • prefer-return-await

    Await returned SDK Futures inside a try body protected by a catch clause.

  • prefer-single-setstateflutter

    Merge consecutive Flutter State.setState statements in the same block.

  • prefer-use-callbackflutter_hooks

    Prefer useCallback for synchronous memoized function literals.

  • prefer-use-memoflutter_hooks

    Prefer memoizing freshly allocated collection values used as hook keys.

  • prefer-using-list-viewflutter

    Suggests considering ListView for a SingleChildScrollView containing a plain Flutter Column with at least two unconditional children in a list literal.

  • sized-box-for-whitespacesized_box_for_whitespacerecommendedflutter

    Flags a Container used only to add fixed width/height whitespace.