avoid-bloc-public-methods
Warns when a Bloc
has public methods except the overridden ones.
When creating a Cubit
, it's recommended to only expose public methods for the purposes of triggering state changes. As a result, generally all public methods on a cubit instance should return void
or Future<void>
.
When creating a Bloc
, it's recommended to avoid exposing any custom public methods and instead notify the bloc of events by calling add.
Example
❌ Bad:
class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0);
// LINT: Avoid declaring public methods for 'Blocs'. Try making them private instead.
void changeSate(int newState) {
state = newState;
}
void onChange(Change<int> change) {
super.onChange(change);
print(change);
}
}
✅ Good:
class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0);
void onChange(Change<int> change) {
super.onChange(change);
print(change);
}
void _changeSate(int newState) {
state = newState;
}
}