內容

避免不必要的容器

避免不必要的容器。

此規則自 Dart 2.7 起可用。

規則集:flutter

此規則提供快速修正

詳細資訊

#

避免將 Widget 包裹在不必要的容器中。

將 Widget 包裹在未設定其他參數的 Container 中沒有任何作用,且會使程式碼不必要地更複雜。

錯誤範例

dart
Widget buildRow() {
  return Container(
      child: Row(
        children: <Widget>[
          const MyLogo(),
          const Expanded(
            child: Text('...'),
          ),
        ],
      )
  );
}

正確範例

dart
Widget buildRow() {
  return Row(
    children: <Widget>[
      const MyLogo(),
      const Expanded(
        child: Text('...'),
      ),
    ],
  );
}

用法

#

若要啟用 avoid_unnecessary_containers 規則,請在您的 analysis_options.yaml 檔案中的 linter > rules 下新增 avoid_unnecessary_containers

analysis_options.yaml
yaml
linter:
  rules:
    - avoid_unnecessary_containers