sort_child_properties_last 린트 규칙: 위젯 생성 시 child 속성은 마지막에 정렬하기

sort_child_properties_last 린트 규칙: 위젯 생성 시 child 속성은 마지막에 정렬하기

위젯 인스턴스를 만들 때 childchildren 같은 자식 속성은 뒤쪽(마지막)에 정렬하도록 권장하는 린트 규칙이에요. 이렇게 하면 가독성이 좋아지고, IntelliJ 같은 에디터의 UI as Code 가이드에서 속성들이 생성자 호출과 명확히 연결돼 보이면서 자식들과 구분되기도 해요.

출처: sort_child_properties_last

본문

설명

위젯 인스턴스를 만들 때는 자식(child) 속성을 마지막에 정렬해요.

이렇게 하면 가독성이 좋아지고, IntelliJ 같은 에디터의 UI as Code 가이드처럼 속성이 올바른 순서로 생성자 호출과 분명하게 연결되어 보이고, 자식들과도 잘 구분되도록 도와주죠.

BAD:

return Scaffold(
  appBar: AppBar(
    title: Text(widget.title),
  ),
  body: Center(
    child: Column(
      children: <Widget>[
        Text(
          'You have pushed the button this many times:',
        ),
        Text(
          '$_counter',
          style: Theme.of(context).textTheme.display1,
        ),
      ],
      mainAxisAlignment: MainAxisAlignment.center,
    ),
    widthFactor: 0.5,
  ),
  floatingActionButton: FloatingActionButton(
    child: Icon(Icons.add),
    onPressed: _incrementCounter,
    tooltip: 'Increment',
  ),
);

GOOD:

return Scaffold(
  appBar: AppBar(
    title: Text(widget.title),
  ),
  body: Center(
    widthFactor: 0.5,
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text(
          'You have pushed the button this many times:',
        ),
        Text(
          '$_counter',
          style: Theme.of(context).textTheme.display1,
        ),
      ],
    ),
  ),
  floatingActionButton: FloatingActionButton(
    onPressed: _incrementCounter,
    tooltip: 'Increment',
    child: Icon(Icons.add),
  ),
);

예외: child 속성 뒤에 함수 표현식(function expression)을 가진 파라미터가 오는 것은 허용돼요.

활성화 방법

sort_child_properties_last 규칙을 활성화하려면 analysis_options.yaml 파일의 linter > rules 아래에 규칙을 추가하면 돼요.

linter:
  rules:
    - sort_child_properties_last

대신 YAML 맵 문법으로 린트 규칙을 설정하고 있다면, linter > rules 아래에 sort_child_properties_last: true를 추가하면 돼요.

linter:
  rules:
    sort_child_properties_last: true

더 알아보기