use_ key_ in_ widget_ constructors
Learn about the use_key_in_widget_constructors linter rule.
Use key in widget constructors.
Details
#DO use key in widget constructors.
It's a good practice to expose the ability to provide a key when creating public widgets.
BAD:
class MyPublicWidget extends StatelessWidget {
}
GOOD:
class MyPublicWidget extends StatelessWidget {
MyPublicWidget({super.key});
}
Enable
#
To enable the
use_key_in_widget_constructors rule, add
use_key_in_widget_constructors
under
linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- use_key_in_widget_constructors
If you're instead using the YAML map syntax to configure
linter rules, add
use_key_in_widget_constructors: true under
linter > rules:
linter:
rules:
use_key_in_widget_constructors: true
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.