use_key_in_widget_constructors
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:
dart
class MyPublicWidget extends StatelessWidget {
}
GOOD:
dart
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:
analysis_options.yaml
yaml
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:
analysis_options.yaml
yaml
linter:
rules:
use_key_in_widget_constructors: true
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2025-03-07. View source or report an issue.