Skip to main content

avoid-bloc-public-fields

added in: 1.25.0
🛠
Free+

Warns when a Bloc or Cubit has public fields.

It's recommended to keep the state or your Blocs private and only update it in event handlers.

Example

❌ Bad:

class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0);

// LINT: Avoid declaring public fields for 'Blocs'. Try making them private instead.
int value = 1;
}

✅ Good:

class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0);

int _value = 1;
}