prefer-correct-edge-insets-constructor
Same as prefer-correct-edge-insets-constructor in Dart Code Metrics
- Group
- Performance
- Path
lint/performance/prefer-correct-edge-insets-constructor- Default
- On (recommended)
- Origin
- Dart Code Metrics
- Domain
- flutter
- Rule
- 336 of 512
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
// 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]);
}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(void testMultipleViolations() { final pad1 = EdgeInsets.only(top: 10, bottom: 10); final pad1 = EdgeInsets.only(top: 10, bottom: 10); final pad2 = EdgeInsets.only(left: 8, right: 8); 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);void containersRegression(int rcount) { final (ra, _) = (EdgeInsets.only(top: 8, bottom: 8), 0); lbl: { final rb = EdgeInsets.only(top: 8, bottom: 8); final rc = switch (rcount) { 0 => EdgeInsets.only(top: 8, bottom: 8), }; final rd = switch (EdgeInsets.only(top: 8, bottom: 8)) { }; final re = [if (rcount > 0) EdgeInsets.only(top: 8, bottom: 8)]; final re = [if (rcount > 0) EdgeInsets.only(top: 8, bottom: 8)]; final rf = [...[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);Valid
// 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.
{
"linter": {
"rules": {
"performance": {
"prefer-correct-edge-insets-constructor": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/performance/prefer-correct-edge-insets-constructor: <reason>More performance rules
- prefer-declaring-const-constructor
prefer_declaring_const_constructorflutterFlags a generative constructor that is legally eligible to be
const. - prefer-foreach
prefer_foreachPrefers
forEachto afor-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
useCallbackfor synchronous memoized function literals. - prefer-use-memoflutter_hooks
Prefer memoizing freshly allocated collection values used as hook keys.
- prefer-using-list-viewflutter
Suggests considering
ListViewfor aSingleChildScrollViewcontaining a plain Flutter Column with at least two unconditional children in a list literal. - sized-box-for-whitespace
sized_box_for_whitespacerecommendedflutterFlags a
Containerused only to add fixedwidth/heightwhitespace.