avoid_public_members_in_states
Severity | Quick Fix | Options |
---|---|---|
Info | ❌ | ❌ |
Details
AVOID declaring public fields and methods in widget state classes.
Public fields and methods with
@visibleForTesting
annotations are ignored. Bad
class _MyWidgetState extends State<MyWidget> {
int counter = 0;
void increment() {
setState(() {
counter++;
});
}
@override
Widget build(BuildContext context) {
return FilledButton(
onPressed: increment,
child: Text('$counter'),
);
}
}
Good
class _MyWidgetState extends State<MyWidget> {
int _counter = 0;
void _increment() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return FilledButton(
onPressed: _increment,
child: Text('$_counter'),
);
}
}