avoid_unnecessary_containers
avoid_unnecessary_containers
아무 설정도 하지 않은 채 Container로 위젯을 감싸는 건 아무 효과가 없으면서 코드만 복잡하게 만들어요. avoid_unnecessary_containers 린트 규칙이 어떻게 이런 불필요한 감싸기를 잡아내는지 살펴볼게요.
본문
상태: 안정(stable) · Flutter 관련 · 자동 수정 가능(Fix available) · 출시: Dart 2.7
위젯을 불필요한 Container로 감싸지 마세요. 다른 파라미터를 전혀 설정하지 않은 Container로 위젯을 감싸는 것은 아무 효과가 없으면서 코드를 쓸데없이 복잡하게 만들 뿐이에요.
나쁜 예
Widget buildRow() {
return Container(
child: Row(
children: [
const MyLogo(),
const Expanded(
child: Text('...'),
),
],
)
);
}
좋은 예
설정할 것이 없는 Container는 그냥 제거해 주세요. 동작은 똑같고 코드는 더 단순해져요.
Widget buildRow() {
return Row(
children: [
const MyLogo(),
const Expanded(
child: Text('...'),
),
],
);
}
규칙 활성화하기
analysis_options.yaml 파일의 linter > rules 아래에 avoid_unnecessary_containers을 추가하면 돼요.
linter:
rules:
- avoid_unnecessary_containers
룰을 YAML 맵 문법으로 설정하고 있다면 linter > rules 아래에 avoid_unnecessary_containers: true를 넣어 주세요.
linter:
rules:
avoid_unnecessary_containers: true