use_key_in_widget_constructors
在 Widget 建構子中使用 key。
詳細資訊
#務必在 Widget 建構子中使用 key。
公開在建立公開 Widget 時提供 key 的能力是一種好的實務。
不良範例
dart
class MyPublicWidget extends StatelessWidget {
}
良好範例
dart
class MyPublicWidget extends StatelessWidget {
MyPublicWidget({super.key});
}
啟用
#若要啟用 use_key_in_widget_constructors
規則,請在您的 analysis_options.yaml
檔案中的 linter > rules 下新增 use_key_in_widget_constructors
analysis_options.yaml
yaml
linter:
rules:
- use_key_in_widget_constructors
如果您改為使用 YAML map 語法來設定 linter 規則,請在 linter > rules 下新增 use_key_in_widget_constructors: true
analysis_options.yaml
yaml
linter:
rules:
use_key_in_widget_constructors: true
除非另有說明,否則本網站上的文件反映的是 Dart 3.7.1 版本。頁面上次更新於 2025-03-07。 檢視原始碼 或回報問題。