Skip to main content

always-pass-global-key

effort: 2m
pro+

Warns when a global key field is not passed to any widget.

Example

❌ Bad:

class _AnotherState extends State<_Example> {
// LINT: This global key is not passed to any widget.
final GlobalKey<String> _formKey = GlobalKey<String>();

void _submitForm() {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
}
}


Widget build(BuildContext context) {
return Form();
}
}

✅ Good:

class _AnotherState extends State<_Example> {
final GlobalKey<String> _formKey = GlobalKey<String>();

void _submitForm() {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
}
}


Widget build(BuildContext context) {
return Form(key: _formKey);
}
}