avoid_public_members_in_states

SeverityQuick FixOptions
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'),
    );
  }
}

Usage

To enable the avoid_public_members_in_states rule, add avoid_public_members_in_states under custom_lint > rules in your analysis_options.yaml file:

custom_lint:
  rules:
    - avoid_public_members_in_states