avoid-unnecessary-containers

Same as avoid_unnecessary_containers in Dart lints

Path
lint/complexity/avoid-unnecessary-containers
Default
On (recommended)
Origin
Dart lints
Domain
flutter
Rule
159 of 512
recommendedflutter

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

example.dartdart
// 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'));
Avoid a `Container` with only a child; use the child directly.
3
4Widget b0() => Container(child: Text('a'));
Avoid a `Container` with only a child; use the child directly.
5
6Widget b1() => Container(child: SizedBox());
Avoid a `Container` with only a child; use the child directly.
8Widget b2() {
9 return Container(
Avoid a `Container` with only a child; use the child directly.
13
14Widget b3() => Container(child: Icon(Icons.home));
Avoid a `Container` with only a child; use the child directly.
15
16Widget b4() => Container(child: Padding(padding: EdgeInsets.zero));
Avoid a `Container` with only a child; use the child directly.
17
18Widget b5() => Container(child: Text('last'));

Valid

example.dartdart
// 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.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "complexity": {
        "avoid-unnecessary-containers": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/complexity/avoid-unnecessary-containers: <reason>