avoid-unnecessary-containers
Same as avoid_unnecessary_containers in Dart lints
- Group
- Complexity
- Path
lint/complexity/avoid-unnecessary-containers- Default
- On (recommended)
- Origin
- Dart lints
- Domain
- flutter
- Rule
- 159 of 512
Flags a Container whose only argument is a child.
A Container that wraps nothing but a child adds a widget to the tree
that neither paints, sizes, nor positions anything — it is pure overhead.
Dropping it and using the child directly shrinks the widget tree, which
helps rebuild performance and readability. The rule fires only when
Container has exactly one named argument, child, and no positional
arguments.
Invalid
// Bad: a Container whose only argument is child.
import 'package:flutter/material.dart';
Widget b0() => Container(child: Text('a'));
Widget b1() => Container(child: SizedBox());
Widget b2() {
return Container(
child: Text('x'),
);
}
Widget b3() => Container(child: Icon(Icons.home));
Widget b4() => Container(child: Padding(padding: EdgeInsets.zero));
Widget b5() => Container(child: Text('last')); Widget b0() => Container(child: Text('a')); Widget b1() => Container(child: SizedBox());Widget b2() { return Container( Widget b3() => Container(child: Icon(Icons.home)); Widget b4() => Container(child: Padding(padding: EdgeInsets.zero)); Widget b5() => Container(child: Text('last'));Valid
// Good: Containers with more than a child, or non-Container widgets.
import 'package:flutter/material.dart';
Widget g0() => Container(width: 10, child: Text('a'));
Widget g1() => Container(color: Colors.red, child: Text('b'));
Widget g2() => Container();
Widget g3() => Container(padding: EdgeInsets.zero, child: Text('c'));
Widget g4() => SizedBox(child: Text('d'));
Widget g5() => Center(child: Text('e'));
Widget g6() => Container(alignment: Alignment.center, child: Text('f'));Configure avoid-unnecessary-containers
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"complexity": {
"avoid-unnecessary-containers": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/avoid-unnecessary-containers: <reason>More complexity rules
- avoid-unnecessary-type-assertions
avoid-unnecessary-type-assertionsrecommendedFlags an
ischeck whose result is already guaranteed by the resolved static type. - avoid-unnecessary-type-casts
avoid-unnecessary-type-castsrecommendedFlags an
ascast to the operand's resolved static type. - cascade-invocations
cascade_invocationsFlags consecutive statements that invoke members on the same variable.
- cognitive-complexity
Reports function bodies with excessive nesting-weighted control flow.
- cyclomatic-complexity
cyclomatic_complexityFlags a function whose cyclomatic complexity exceeds the configured threshold.
- join-return-with-assignment
join_return_with_assignmentFlags an assignment immediately followed by
returnof the same variable. - max-lines-for-file
max_lines_for_fileFlags a file longer than the configured line limit.
- max-lines-for-function
max_lines_for_functionFlags a function or method longer than the configured line limit.